Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange

 
amit mawkin
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please consider this code
<code>

public class Test006 extends Object
{
static int staticTotal = 10;
int nonStaticTotal = 20;
static void method1()
{
staticTotal = 30; //1
Test006.staticTotal = 30; //2
new Test006().staticTotal = 30; //3
}
void method2()
{
staticTotal = 30; //4
this.staticTotal = 30; //5
Test006.staticTotal = 30; //6
new Test006().staticTotal = 30; //7
}
}

</code>
The strange part that I feel is that this code compiles.
in method2() please look at line marked 4 clearly showing a non static metod trying to call a static value.
The mock from where I picked this questions asks which marked lines will not compile.
In ans it was only stated nothing is wrong with the code
which is indeed a fact.
can anyone clear my doubts
------------------
coffee drinker
[This message has been edited by amit mawkin (edited December 07, 2001).]
[This message has been edited by amit mawkin (edited December 07, 2001).]
[This message has been edited by amit mawkin (edited December 07, 2001).]
[This message has been edited by amit mawkin (edited December 07, 2001).]
[This message has been edited by amit mawkin (edited December 07, 2001).]
[This message has been edited by amit mawkin (edited December 07, 2001).]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep the answer is correct. There is nothing wrong in accessing a static member from a non-static method. What is wrong is trying to do the opposite: accessing non-static member from static methods. An instance can access all information about the class (static info) it wants to. But the class by itself cannot access instance info (non-static info). You have to really make the difference between a class and an instance of a class. Once you get the concept, you'll see those question won't be a probleme anymore.
Here is an informal example. Consider the class Cat. Also consider two Cat instances, Bob and John (weird name for cats ). Now the class Cat knows nothing about (non-static) members of Bob and John. That means that a class (static) cannot access non-static members.
Bob and John, in the contrary, know that a Cat has four legs (nbLegs is a static member of class Cat). That means that an instance (non-static) can access static info (class info).
That's maybe not the best example but the only that came up so early on a Friday morning .
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited December 07, 2001).]
 
amit mawkin
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Val
gosh i dint knew that.
------------------
coffee drinker
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JLS 8.4.3.2

A method that is declared static is called a class method. A class method is always invoked without reference to a particular object. An attempt to reference the current object using
the keyword this or the keyword super in the body of a class method results in a compile-time error. It is a compile-time error for a static method to be declared abstract.
A method that is not declared static is called an instance method, and sometimes called a non-static method. An instance method is always invoked with respect to an object,
which becomes the current object to which the keywords this and super refer during execution of the method body.

HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic