File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Question about classes and static keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about classes and static keyword" Watch "Question about classes and static keyword" New topic
Author

Question about classes and static keyword

Philip Herzer
Greenhorn

Joined: Oct 21, 2003
Posts: 21
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

Thanks,
Phil

card.java:


onecard.java:

[ January 06, 2005: Message edited by: Philip Herzer ]

"Mejor morir de pie que vivir toda la vida arrodillas."<br /> Emilio Zapata
durga kar
Greenhorn

Joined: Jan 06, 2005
Posts: 5
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.
Philip Herzer
Greenhorn

Joined: Oct 21, 2003
Posts: 21
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 ]
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I would say that it is better to first think of the class and objects of that class (instances) as different things.

The static keyword makes that method/variable part of the class and not part of the instances.

All instances have access to the fields of the class, but a class does not have access to the fields of an instance unless it creates or is passes an instance.

You should always access a static method or variable through it's class

Class.staticMethod();

rather than

Class instance = new Class();
instance.staticMethod();
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Philip Herzer:
... So I can apply this to constant variables and methods that I do not want to change, right?

Yes, but keep in mind that static variables are not necessarily final.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
Consider Paul's rocket mass heater.
 
subject: Question about classes and static keyword