Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about classes and static keyword

 
Philip Herzer
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
durga kar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic