About Question enthuware.ocajp.i.v7. 2 . 1220 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
subhamsdalmia
Posts: 32
Joined: Sat May 02, 2015 11:57 pm
Contact:

About Question enthuware.ocajp.i.v7. 2 . 1220 :

Post 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!

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1220 :

Post 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.

subhamsdalmia
Posts: 32
Joined: Sat May 02, 2015 11:57 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1220 :

Post 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();

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1220 :

Post 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.

subhamsdalmia
Posts: 32
Joined: Sat May 02, 2015 11:57 pm
Contact:

Re: About Question enthuware.ocajp.i.v7. 2 . 1220 :

Post 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);
}
}

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7. 2 . 1220 :

Post 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.

subhamsdalmia
Posts: 32
Joined: Sat May 02, 2015 11:57 pm
Contact:

Re: About Question enthuware.ocajp.i.v7. 2 . 1220 :

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests