• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

referenc static variables with this in methods

 
jason stark
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm preparing for the Sun Certified java exam. I've tried refencing a static variable from within a method using "this" and it worked. I didn't expect it to as the variables not attached to an object. Also I've managed to compile transient variables with the prefixed by the word static which I read was not possible.
Also some times I've seen this as legal:-
public static int main(String[] args)
will this be legal for the exam
thanks for any help Jason
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jason
Conserning the 2nd question about
public static int main(String[] args) {}:
First of all.. in the exam you have to use what the JLS says - not what the compiler states. And acording to the JLS the "main method" (the one that's called with "java ClassName") has to be:
public static void main(String[] args)
(as a side note, it can also be final)
btw.
It's perfectly okay to make a class with the above public static int main(String[] args) signature.
Eg.

will compile with no errors - but if you try to execute the file you'll get a java.lang.NoSuchMethodError: main
Exception (cause it doesn't have a main method with the correct signature).
/Svend Rost
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Svend, also
If you put both methods in a file,
public static void main (String [] args) {}
public static int main (String [] args) {}
you'll get a compiler error:

You cannot overload a method by changing the return type ONLY.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding your first question, nothing prevents you from referencing a static variable with "this", that is, prefixing the variable id with this. The type of the object referenced by "this" (i.e. an object of the class in which "this" is used or any subclass) will be used to resolve the static variable. Nothing is wrong about it, although the practice is not that good. You should always reference a static variable with the type the variable is declared in.
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add, what's not allowed is to try to use the this keyword in a static method since static methods are not passed the this object reference... right??
[ December 02, 2002: Message edited by: Alfred Kemety ]
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[Code reformatted by Val]
[ December 03, 2002: Message edited by: Valentin Crettaz ]
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
within an instance method, called with david.instanceMethod1() for instance,
I can access other objects of the same class, say the terri instance by the following:
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The type of the object referenced by "this" (i.e. an object of the class in which "this" is used or any subclass) will be used to resolve the static variable.

A minor slip: variables are not resolved at runtime, the compile type of the variable used in the access expression is searched for declared or inherited fields, but not the type of the object pointed to by the variable.
This means this code prints 0 because the compile type of "this" is Test:
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jose Botella:

This means this code prints 0 because the compile type of "this" is Test:

Jose:
Wow - if this had been a test question, I would have got it wrong!
I think what threw me off is that I am not used to seeing the same variables that are declared in the superclass, redeclared in the subclass as is in the case above.
So I am not sure what behaviour this would cause.
Anyways, since I plan on inheriting things in the subclass, I rewrote the code to the following:

Your code and information was a real eye opener though!
 
jason stark
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by david eberhardt:
<hr></blockquote>
Jose:
Wow - if this had been a test question, I would have got it wrong!
I think what threw me off is that I am not used to seeing the same variables that are declared in the superclass, redeclared in the subclass as is in the case above.
So I am not sure what behaviour this would cause.
Anyways, since I plan on inheriting things in the subclass, I rewrote the code to the following:

Your code and information was a real eye opener though![/QB]

Hi I tried this bit of code and removed the this bit removed and it made no diffence to what was printed.
Thanks to everyone who replied Jason
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic