Hey all. I would like to find out how i can access/ get the value of a variable in a different class. this is part of the code which shows what I was trying to do.

public class A{

	protected int x;
	
          //missing code
	C obj = new C (); 
	C.x=x; //i get an error "indentifier expected"
}

class C extends B{
protected int x;
	public int getInt()
	{
		System.out.print("Enter Value ");
		x=scan.nextInt();
	        return x;
	}
}

Recommended Answers

All 2 Replies

Hello,
You need to change your class A, because you have two problems:
1st - you don't have method at class A. you need to put your code inside one method
2nd - you can't access a instance variable like a class variable (static)

this code should work:

public class A{

    protected int x;
   ->  public void test(){     
    C obj1 = new C ();
   -> obj1.x = x;
    }

I hope that helps you.

yep. that helped.. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.