Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what the difference?

Status
Not open for further replies.

porkymin

Programmer
Dec 26, 2004
16
0
0
SG
hi all,

Was trying to figure out what is the difference between
public static boolean workday(Collection colworkday)
and public boolean workday(Collection colworkday)?
Any kind help is greatly appreciated.
 
One is a static method, and one is not.

So with a static method you can do :

MyClass.callMethod();

but with a non-static method, you must instantiate a class before you call a method :

MyClass my = new MyClass();
my.callMethod();

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Thank u Diancecht and sedj!
Question i have:
1) what do u mean by instance methods?
Meaning if it is with static we can only call that
method?
2) Suppose the following scenario:

Suppose a java class : MyClass
- used to call public static boolean method1() in MyHome

MyClass will initialise a new "MyHome" object

private static MyHome h = new MyHome();

then calls MyHome.method1() in MyClass

What if we did this change: remove the static from method1() to be

Can MyClass initialise new MyHome object and calls its method1() to become public boolean method1(),
will it still work?



 
When you are instantiating the class with
Code:
MyHome h = new MyHome();
You are creating an instance of MyHome which is referenced by h. Therefore,
Code:
h.method1();
calls the method of the object that has been created. On the other hand,
Code:
MyHome.method1()
will call a static (unchanging) method. The difference will become clear when you start adding multiple instances of MyHome.
Code:
MyHome h1 = new MyHome();
MyHome h2 = new MyHome();
Every object instantiated from a given class has its own copy of each instance variable. Therefore, there are two instances of MyHome which are h1 and h2, but they both share the common class MyHome. MyHome can have static methods that only have access to static fields in MyHome, but h1 and h2 can be manipulated separatly.
 
1) Instance methods = non static methods.
2) No, it won't work. But the best way to kno is trying it. It won't take more that 10 mins.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top