0

I have defined an object and declared a static variable i. In the get() method, when I try to print the instance and class variable, both print the same value.

Isn't this.i an instance variable? Should it print 0 instead of 50?

public class test {
    static int i = 50;
    void get(){
        System.out.println("Value of i = " + this.i);
        System.out.println("Value of static i = " + test.i);
    }

    public static void main(String[] args){
        new test().get();
    }

}
Tuna
  • 2,937
  • 4
  • 37
  • 61
user1050619
  • 19,822
  • 85
  • 237
  • 413
  • A `static` variable is declared on class level, not instance. It will always be the same for each instance. It can be referenced as `this.VARIABLE` or `Type.Variable`. – Jeroen Vannevel Sep 26 '13 at 18:32
  • 1
    As an aside, even for test code it's best to follow naming conventions. It would be more obvious what's going on if your class were called `Test` rather than `test`. – Jon Skeet Sep 26 '13 at 18:33

4 Answers4

8

No, there's only one variable - you haven't declared any instance variables.

Unfortunately, Java lets you access static members as if you were accessing it via a reference of the relevant type. It's a design flaw IMO, and some IDEs (e.g. Eclipse) allow you to flag it as a warning or an error - but it's part of the language. Your code is effectively:

System.out.println("Value of i = " + test.i);
System.out.println("Value of static i = " + test.i);

If you do go via an expression of the relevant type, it doesn't even check the value - for example:

test ignored = null;
System.out.println(ignored.i); // Still works! No exception

Any side effects are still evaluated though. For example:

// This will still call the constructor, even though the result is ignored.
System.out.println(new test().i);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Technically, you cannot access a static member with a reference. Rather, there's a bit of "syntactic vinegar" in javac that will let you reference a static using the DECLARED TYPE of a reference variable -- the value/actual class of the variable is irrelevant (and I haven't tried but it may be possible for it to be null). – Hot Licks Sep 26 '13 at 18:37
  • @HotLicks: Yes, the value is irrelevant - but it's via an expression which would be a reference. It's an expression of that type. Will edit, but I think it's reasonably clear already. – Jon Skeet Sep 26 '13 at 18:38
  • Thanks for checking that null thing! – Hot Licks Sep 26 '13 at 18:53
  • My favorite example of why this design decision is awful is `Thread.Sleep`. – Brian Sep 27 '13 at 17:32
  • @Brian: Yes, me too - I've used that as an example in many places :) – Jon Skeet Sep 27 '13 at 17:36
2

The field i is declared as static. You can access static fields either with the YourClass.StaticField or instance.StaticField. So both of

this.i
test.i

are referring to the same value in the context of an instance method of your test class.

It's considered bad practice to access a static field with this.i or instance.i.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

static is a class level variable and non static is an instance variable(object level variable) . So here you declare only static variable and call them different way but same meaning.

this.i
test.i

both treated as class level variable or static variable.

Sunil Behera
  • 9
  • 1
  • 3
0

you didn't declare any instance variable in here.only one static variable.if you declare instance variable without assigning value,then if you try to print that instance variable value using "this" key word you can get default value as 0.