This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Calling static method from main Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Calling static method from main" Watch "Calling static method from main" New topic
Author

Calling static method from main

SimonJ Birch
Greenhorn

Joined: Jun 01, 2006
Posts: 10
I've got a query regarding calling a static method from the main method. Why in this program is the method FizzSwitch() static.....

class Fizz {
int x = 5;
public static void main(String[] args) {
final Fizz f1 = new Fizz();
Fizz f2 = new Fizz();
Fizz f3 = FizzSwitch(f1,f2);
System.out.println((f1 == f3) + " " + (f1.x == f3.x));
}
static Fizz FizzSwitch(Fizz x, Fizz y) {
final Fizz z = x;
z.x = 6;
return z;


....when in this program....


class Eggs {
int doX(Long x, Long y) { return 1; }
int doX(long... x) { return 2; }
int doX(Integer x, Integer y) { return 3; }
int doX(Number n, Number m) { return 4; }
public static void main(String[] args) {
new Eggs().go();
}
void go() {
short s = 7;
System.out.print(doX(s,s) + " ");
System.out.println(doX(7,7));
}
}


... the method go() isn't static even though it is being called from main?

Thanks

Simon
shuba karthik
Greenhorn

Joined: Sep 03, 2004
Posts: 14
Hi,

Without an instance of an object you can never access a non static method from a static context.
In the second program new Eggs() will create an instance of Eggs.
In other words you have created an object of Eggs.
Hope this clarifies your doubt.

Regards,
Subhakarthik
SimonJ Birch
Greenhorn

Joined: Jun 01, 2006
Posts: 10
That makes it clearer.

Thanks Shuba


Simon
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Calling static method from main
 
Similar Threads
help me in understanding this program
chapter 3 self test question 5, K&B
Passing References
Final
explain this code