-3

I am a new-bee to Java.

I know, even a sub-class can not refer a non-static member of a base class directly. like,

class BaseClass
{
int id;
public void testMethod()
{
    System.out.println("Hello");
}
}
public class Test1 extends BaseClass
{
public static void main(String[] args)
{
    System.out.println("ID : " + id);
}
}

This will give us an error "Cannot make a static reference to the non-static field id"

But, in case of abstract class, we can do it.

abstract class MyAbstractClass
{
int id;
public void setId(int id)
{
    this.id = id;
}
}

public class SubClass extends MyAbstractClass
{
public void testMethod()
{
    System.out.println("ID Value : " + id);
}
public static void main(String[] args)
{
    SubClass obj = new SubClass();
    obj.setId(1);
    obj.testMethod();
}
}

I was wondering how and why is it possible in case of abstract class. Appreciate your answers. Please be gentle, I am a new-bee to java. :)

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Touhid K.
  • 351
  • 1
  • 5
  • 23
  • Please provide compilable code so we can understand what you are doing. – Sotirios Delimanolis May 15 '14 at 15:06
  • 4
    This is uncompilable code. You cannot directly call a method in the class definition, it should be done inside a method. – Luiggi Mendoza May 15 '14 at 15:06
  • Note that you don't have a single `abstract` class. – Jeroen Vannevel May 15 '14 at 15:07
  • @JeroenVannevel OP should edit the code accordingly, not us. – Luiggi Mendoza May 15 '14 at 15:07
  • @LuiggiMendoza: I didn't do any code changing, only indentation(except syso) – Jeroen Vannevel May 15 '14 at 15:08
  • 1
    @JeroenVannevel yes, the `syso` is code change. If you will edit, do the indentation only. – Luiggi Mendoza May 15 '14 at 15:09
  • 6
    `syso` is a shortcut in Eclipse, I find it unreasonable to revert my edit just for that. If you do, then I suggest you take my edit and put it back in but at least leave the other changes intact. – Jeroen Vannevel May 15 '14 at 15:10
  • 2
    Your example is simply wrong. You will only get the error you stated, if you try to access the instance variables from a static context. Therefore your subclass or the method within your subclass must be static to get this error. – wumpz May 15 '14 at 15:14
  • @JeroenVannevel I find that every bad piece of code should be edited by OP only, even if its a shortcut it is **bad** to post it here because it is **uncompilable** and we have to provide a **Short, Self Contained, Correct (Compilable), Example** piece of code, regardless the IDE we use or not. By the way, `syso` works on Eclipse IDE but not on Idea (and I prefer to work on Idea rather than Eclipse), and I don't know if `syso` could be replaceable in Netbeans or other IDE's as well. – Luiggi Mendoza May 15 '14 at 15:15
  • 1
    OK, now that you've shown us a complete example: This has nothing to do with `abstract`. In the first example, you're referencing `id` from a static method, and in the second, you aren't. – ajb May 15 '14 at 15:16
  • You stated _But, in case of abstract class, we can do it._, but haven't demonstrated it. – Sotirios Delimanolis May 15 '14 at 15:17
  • @LuiggiMendoza: If you can edit out arbitrary constraints (such as "you must be using Eclipse in order for this to compile because it has a shortcut that I've used") whilst otherwise maintaining the integrity of the testcase, then you should absolutely go right ahead. Anyway, it looks like you probably agree with me on that and think that Jeroen was trying to add in `syso`, whereas in fact it's the opposite: the OP had written `syso` and Jeroen was correcting it to valid Java, because this clearly wasn't the question at hand. – Lightness Races in Orbit Jun 14 '14 at 22:11

2 Answers2

3

The problem you stated does not depend on abstract classes but on static and nonstatic contexts. You simply make two different calls to the instance variable.

The first example tries to access id from a static context which is not allowed and you get your error (Cannot make a static reference to the non-static field id).

public static void main(String[] args)
{
    System.out.println("ID : " + id);
}

In the second example, you create an instance of your class and from within this instance you are able to access id. This is a reference from a nonstatic context.

public void testMethod()
{
    System.out.println("ID Value : " + id);
}

So you achieve the same for your first example, if you create an instance like in the second and access id from a nonstatic method..

wumpz
  • 8,257
  • 3
  • 30
  • 25
  • Hmmm...Thats what I understood now. :) Thanks by the way. – Touhid K. May 15 '14 at 15:24
  • 1
    No problem. Please provide better examples the first time to avoid the "format", "non compilable" comments. So you will get a better and faster feedback. – wumpz May 15 '14 at 15:27
1

Cannot make a static reference to the non-static field id

This error appears because you want to access to a non-static field inside a static method. And that's what you're doing here: main method is static, but id is not.

public static void main(String[] args) {
    //here you don't create an instance of the class
    //you cannot access to id directly
    System.out.println("ID : " + id);
}

You need to create an instance of the class to access to its non-static fields, as you did in the second example, regardless if you're extending from other (abstract) class or not.

public static void main(String[] args) {
    //here you create an instance of the class
    SubClass obj = new SubClass();
    obj.setId(1);
    obj.testMethod();
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332