This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
My question is referring to this following code, based on a very simple card game. I was wondering if it is more common to access and instatiate members of an object referring to a type class card by directly referencing the reference variable p1 & p2, like I have done in onecard.java? (referenceVariableName.memberName)
My instructor is insisting that we include the name of our class, for example: card p1 = new card(); card.init(gen,p1) // instead of p1.init(gen,p1)
Second question, can somebody put into lamen terms what the 'static' keyword is and when to use it? I noticed that variables outside of Main() require the static require that keyword
[ January 06, 2005: Message edited by: Philip Herzer ]
"Mejor morir de pie que vivir toda la vida arrodillas."<br /> Emilio Zapata
Look a static keyword in front of a member varialble is to make it global and should be accessed by the name of its Class. as ClassName.memberName And moreever a static method can access another static method only i.e. the reason why you should declare a member static when you want to access it directly in main(). Your answer to first question also lies here.
Joined: Oct 21, 2003
CAn I think of the static keyword as:
1) A global members common to all instances of that class. 2) a static variable or method can be accesed outside of a class eventhough no instances of that class exist.
So I can apply this to constant variables and methods that I do not want to change, right?
Can someone give me a situation when to use a static method?
Also, can someone comment on my first question in this post? Thanks. [ January 06, 2005: Message edited by: Philip Herzer ]
Originally posted by Philip Herzer: My instructor is insisting that we include the name of our class, for example: card p1 = new card(); card.init(gen,p1) // instead of p1.init(gen,p1) [...] card p1 = new card(); card p2 = new card(); p1.init(gen,p1); p2.init(gen,p2);
Your instructor is correct. While the compiler will allow it with a warning, it's misleading. Note that in the two lines of code above, both calls to different instances of the card class are actually made to the single card class. The following is equivalent, but you can see how it's misleading.The reason it's equivalent is that the instance is ignored when calling a static method. Instead, only the type of the reference that points to the instance is used by the compiler to determine which method to call. This is why there is no polymorphism/overriding of static methods in Java.
Your two stipulations of static members are correct, though Steven makes a good point to think of the static members as belonging to the class itself. It only makes a difference when you consider your second point: that no instances need exist for those members to be accessible.
As to when to use static methods, first I'd say that the init() and print() methods above are clearcut cases of when not to use a static method. Note that the body of the methods exclusively operate on instance members of the card argument. Instead, make them non-static, remove the card arguments and replace all instances of "c." with "this." (or remove them if you like).
Positive examples include factory methods (methods in class A that create instances of class B without the need of an instance of A), singleton management (ensuring there is exactly one instance of class A in the JVM) and utility methods (e.g. java.util.Math). In typical applications instance methods far outweigh static methods.