0

Possible Duplicate:
What is the reason behind “non-static method cannot be referenced from a static context”?

I am new to Java. I have following sets of code as below.

class Default
{
    private short s;
    private int i;
    private long l;
    private float f;
    private double d;
    private char c;
    private String str;
    private boolean b;

    public static void main (String args[ ])
    {

        Default df = new Default();

        System.out.println("\n Short = "+s);

        System.out.println ("\n int i =" + i);
        System.out.println ("\n long l =" + l );
        System.out.println ("\n float f =" + f);
        System.out.println ("\n double d =" + d);
        System.out.println ("\n char c =" + c);
        System.out.println ("\n String s =" + str);
        System.out.println("\n boolean b =" + b);
    }
}

This produces an error message as the subject of this question but following code works perfectly.

class Default
{
    private short s;
    private int i;
    private long l;
    private float f;
    private double d;
    private char c;
    private String str;
    private boolean b;

    public static void main (String args[ ])
    {

        Default df = new Default();

        System.out.println("\n Short = "+df.s);

        System.out.println ("\n int i =" + df.i);
        System.out.println ("\n long l =" + df.l );
        System.out.println ("\n float f =" + df.f);
        System.out.println ("\n double d =" + df.d);
        System.out.println ("\n char c =" + df.c);
        System.out.println ("\n String s =" + df.str);
        System.out.println("\n boolean b =" + df.b);
    }
}

This gives the desired result. What is the difference in these two set of code.

Community
  • 1
  • 1

2 Answers2

2

You have an instantiated object of Default named df which is calling those variables. Since the variables you created are not static, they must be apart of some object that has been created.

Default df = new Default();
df.i //<- is now a part of the instantiated object df.

You use the keyword static if it is not used with an object. So you could just say:

private static char c;

And then you can call char c anytime, which will be null because you haven't given it a value yet.

You use static when you will be using that variable or method without an object.

Be Brave Be Like Ukraine
  • 7,596
  • 3
  • 42
  • 66
shaunw
  • 360
  • 1
  • 3
  • 10
0

The difference between the two sets of code is that in the second example you have an instance of your class, which you create when you say Default df = new Default();.

You've declared all the variables in the Default class as instance-level variables, which means that in order to access them you must go through a particular instance of your class. Or to put it another way, each instance of your class has its own set of variables, so the only way for the compiler to know which one you are actually accessing is if you specify the instance you want to use first.

For example, you could do:

Default df = new Default();
Default anotherOne = new Default();

df.s = 1;
anotherOne.s = 2;

System.out.println("\n Short(df) = "+df.s);                  //'1'
System.out.println("\n Short(anotherOne) = "+anotherOne.s);  //'2'

The problem with attempting to access instance variables directly from main() (or from any other static method) is that the main() method is not directly associated with any instance of your class. So in that case, the compiler doesn't know which instance to fetch the variable from, unless you explicitly specify one.

You could, of course, make your variables static, but then instead of one set of values per instance you'd just have one set of variables for the class, no matter how many instances of it you create. In such a case, the code in the example about would still compile and run (although it would/should produce compiler warnings), but both statements would print '2' because there'd only be a single s variable instead of one for each instance of the class.

aroth
  • 54,026
  • 20
  • 135
  • 176