Methods in Java are not static by default — they are instance methods by default
When you write a method in Java without the static keyword, that method belongs to an object instance, not to the class itself. This is the default behavior. A static method, by contrast, belongs to the class and runs the same way no matter which object calls it — or even if no object exists yet.
The confusion often comes from how Java handles the main method, which must be static because the program needs to run it before any objects are created. But that is a special case, not the default rule for all methods.
Key Takeaways
- Instance methods (the default) are tied to a specific object and can access that object's data through this.
- Static methods belong to the class itself and cannot access instance data because they do not run on any particular object.
- The main method must be static because it runs before any objects exist, but this does not make static the default for other methods.
- Choosing between instance and static methods depends on whether the method needs to work with a specific object's data or just perform a general task.
How Instance Methods Work by Default
When you create a method without the static keyword, Java treats it as an instance method. Each time you create an object from the class, that object gets its own copy of the method to call. The method can read and change the object's data because it has access to this — a reference to the specific object running it.
For example, if you have a class called BankAccount with a method called withdraw, each BankAccount object can call withdraw on itself. The method knows which account's balance to reduce because it is tied to that specific object. This is why instance methods are the default — most methods need to work with an object's own data.
When and Why You Use the Static Keyword
You add the static keyword when a method does not need to work with any particular object's data. A static method belongs to the class as a whole, not to any instance. You can call it directly on the class name without creating an object first.
The main method is static because the Java runtime needs to start your program before any objects exist. It calls main on the class itself, not on an object. Utility methods — like a Math.sqrt() function that just does a calculation — are also often static because they do not need to remember anything about a specific object.
The Difference in How They Access Data
An instance method can use this to refer to the object it is running on. If your class has a field called balance, an instance method can read it, change it, or pass it to other methods. A static method cannot do this because it is not running on any object — there is no this to refer to.
A static method can only access other static fields and static methods in the class. If you try to use an instance field inside a static method, Java will not compile the code. This restriction exists because a static method might be called before any objects are created, so there would be no instance data to access.
Common Mistakes When Choosing Between Them
Beginners sometimes make a method static when it should be an instance method, usually because they see main is static and assume that is the pattern. This breaks the method's ability to work with object data. If your method needs to read or change the object's fields, it must be an instance method.
The reverse mistake is rarer but happens: trying to make a utility method an instance method when it should be static. If a method does not use any of the object's data and just performs a general calculation or task, making it static saves memory and makes the code clearer about what the method does.
How This Affects the Way You Call Methods
You call an instance method on an object: myAccount.withdraw(50). You call a static method on the class: Math.sqrt(16) or MyClass.helperMethod(). This difference in syntax reflects the difference in what the method is — one belongs to an object, the other to the class.
If you try to call a static method on an object, Java will allow it but will actually call the class version, not an object-specific version. This works but is confusing to read, so good practice is to call static methods on the class name instead.
Frequently Asked Questions
Can a static method call an instance method?
No, a static method cannot call an instance method directly because it has no object to call it on. A static method can create an object first, then call the instance method on that object. But if the static method just needs to call another method without an object, that other method must also be static.
Why does the main method have to be static?
The Java runtime starts your program by calling main on the class itself, before any objects are created. Because no object exists yet, main must be static. Inside main, you can create objects and call their instance methods.
If I do not write static or instance, what is the default?
The default is instance. If you write a method without the static keyword, Java treats it as an instance method that belongs to objects, not to the class. You must explicitly add static if you want the other behavior.
Can I have both a static and instance method with the same name?
Yes, Java allows this through a feature called overloading. You can have myMethod() as an instance method and myMethod() as a static method in the same class. Java knows which one to call based on whether you call it on an object or on the class name. However, this is confusing and most programmers avoid it.