Static Methods

Greedy thomas

Ranch Hand

Posts: 37

posted 14 years ago

Hello Consider the following code below : public class Second {

public void aMethod (int aInt) { System.out.println("In aMethod") ; for (int i = 1; i < 3; i++) System.out.println(i) ; } public static void main (String[] args) { System.out.println("Going to call aMethod") ; int a = 5 ; aMethod(a) ; System.out.println("Back from aMethod") ; } } When I compile this class I get an error stating that a non-static method cannot be referenced from a static context. I then changed the method to be 'public static void aMethod ...' and it worked fine. My question is : what did the error message mean and why did it work when I added static to the method definition. Thanks In Advance, Rajesh

Michael Morris

Ranch Hand

Posts: 3451

posted 14 years ago

Hi Rajesh, Welcome to JavaRanch. A static context can be considered as a classwide context or more simply, a static variable or method is the same for all instances of the class and are accessible to other classes without instantianting an object of the class containg the static members. In your example, main is static. If you attempt to call a method in the Second class that is not static, then the problem for the compiler is which instance of Second does it use? By making aMethod() static then there is only one so the compiler has no problem in determining your intention. Now you could have done this without declaring aMethod() static:

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher

Stevie Aldrich

Greenhorn

Posts: 2

posted 14 years ago

public class Second { public void aMethod (int aInt) { System.out.println("In aMethod") ; for (int i = 1; i < 3; i++) System.out.println(i) ; } public static void main (String[] args) { System.out.println("Going to call aMethod") ; int a = 5 ; aMethod(a) ; System.out.println("Back from aMethod") ; } } Main holds no instance of an object of class Second, therefore it can't possibly call a method within class Second. (There's no place where aMethod exists in memory, no entry point to the code.) When aMethod within class Second is declared static, then there is always an entry point for that method, even without an instance of that class. You can call aMethod from within main with Second thing = new Second(); thing.aMethod(a);

Greedy thomas

Ranch Hand

Posts: 37

posted 14 years ago

Originally posted by Stevie Aldrich: public class Second { public void aMethod (int aInt) { System.out.println("In aMethod") ; for (int i = 1; i < 3; i++) System.out.println(i) ; } public static void main (String[] args) { System.out.println("Going to call aMethod") ; int a = 5 ; aMethod(a) ; System.out.println("Back from aMethod") ; } } Main holds no instance of an object of class Second, therefore it can't possibly call a method within class Second. (There's no place where aMethod exists in memory, no entry point to the code.) When aMethod within class Second is declared static, then there is always an entry point for that method, even without an instance of that class. You can call aMethod from within main with Second thing = new Second(); thing.aMethod(a);

Thanks Stevie I understand what you are saying but I think that my confusion from this : aMethod is defined within the class Second so I assumed that it would be available to main() also because it is declared before main(). That was why I could not understand why the compiler was complaining. I have read the replies regarding static methods but I suppose I still need to practise to get used to it. PS : What is the relationship between main() and class Second. I know that there can be only one main method per class - is there anything else I should know. Rajesh

John Uhlig

Greenhorn

Posts: 13

posted 14 years ago

Rajesh, As I am finding out, there is LOTS to know about the main method. Do you have a book you are working from? And/or have you gone through the tutorial on Sun's web site? http://java.sun.com/docs/books/tutorial/index.html One book that is free is Bruce Eckel's "Thinking in Java". You can download it off the web for free. A for some specifics about _the_ main method: (from "Sun Certified Programmer and Developer for Java 2 Study Guide", pg. 111) - it must be static - it must have a void return type - it must have a single String array argument - you can name the argument anything you want - it should be declared public Hope this helps. John

Michael Morris

Ranch Hand

Posts: 3451

posted 14 years ago

PS : What is the relationship between main() and class Second. As far as class Second is concerned or any class for that matter, main() has no special meaning at all. It is merely a public static method with a single String[] array for its signature with no return. It does have special meaning to the JVM. It is the entry point for a java application. The JVM gathers the command line parameters and calls main() to start your program.I know that there can be only one main method per class - is there anything else I should know. Not necessarily true. You can have overloaded main() methods just like any other method. This is perfectly legitimate:

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher

Originally posted by R Mera Naam: aMethod is defined within the class Second so I assumed that it would be available to main() also because it is declared before main(). That was why I could not understand why the compiler was complaining.

When a method call does not have an object that it is being invoked on, an implicit this is added. So, even in static contexts, when you say: aMethod(a); it gets translated to: this.aMethod(a); (As an aside, I always include the "this." when making method calls. Som epeople disagree with this and say that it creates unnecessary code, but I think it makes the code more readable.) The static methods are not procedure-oriented code, so order-of-declaration does not matter. It would be more appropriate to think of them as being methods that belong to X.class (although they are not truly; otherwise there would be a "this", the instance of the class, like in Objective-C)