0

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;
	}
}
2
Contributors
2
Replies
7
Views
9 Years
Discussion Span
Last Post by joshmo
0

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.

Edited by Nick Evan: Fixed formatting

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.