Win a copy of Functional Reactive Programming this week in the Other Languages forum!
    Bookmark Topic Watch Topic
  • New Topic

Static Methods

 
Greedy thomas
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Report post to moderator
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:
 
Stevie Aldrich
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Report post to moderator
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:
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Report post to moderator
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)
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Report post to moderator
Folks, you need to post in other threads to win a book, so I am closing this thread.
 
    Bookmark Topic Watch Topic
  • New Topic