Page 1 of 1
About Question enthuware.ocajp.i.v7. 2 . 1220 :
Posted: Wed May 20, 2015 1:58 am
by subhamsdalmia
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static field b
at TestClass.C.main(C.java:18)
Sir, can you please make changes for the above program to access "i" declared in class B!
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Wed May 20, 2015 2:56 am
by admin
I have no idea what you are trying to do. Please read the detailed explanation given with the question carefully. Also, my comment above explains exactly how you can access b's i.
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Wed May 20, 2015 3:13 am
by subhamsdalmia
admin wrote:I have no idea what you are trying to do. Please read the detailed explanation given with the question carefully. Also, my comment above explains exactly how you can access b's i.
I have tried, B b = new B();
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Wed May 20, 2015 3:30 am
by admin
You have some problem in your environment. Please fix that issue first. If you are using any IDE, please don't use it. Just use the command line.
Re: About Question enthuware.ocajp.i.v7. 2 . 1220 :
Posted: Wed May 20, 2015 3:37 am
by subhamsdalmia
This is the code that I am trying to compile,
And yes I am using IDE.
class A{
private int i = 10,j=0;
public void f(){}
public void g(){}
}
class B extends A{
public int ib = 20;
public void g(){}
}
public class C{
A a = new A();//1
B b = new B();//2
public static void main(String[] args) {
System.out.println(b.ib);
}
}
Re: About Question enthuware.ocajp.i.v7. 2 . 1220 :
Posted: Wed May 20, 2015 3:53 am
by admin
As I said, stop using IDE. Use the command line.
I have removed your message about compilation issue from other thread because it has nothing to do with that question.
Re: About Question enthuware.ocajp.i.v7. 2 . 1220 :
Posted: Thu May 21, 2015 10:41 am
by subhamsdalmia
Code: Select all
class A
{
private int i = 10,j=0;
public void f(){}
public void g(){}
}
class B extends A
{
public int ib = 20;
public void g(){}
}
public class C
{
A a = new A();//1
B b = new B();//2
public static void main(String[] args)
{B b = new B(); System.out.println(b.ib);}
}
This helped finally.