Accessing variables of another class

Thread Solved

Join Date: Oct 2007
Posts: 280
Reputation: joshmo is an unknown quantity at this point 
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

Accessing variables of another class

 
0
  #1
Oct 27th, 2008
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.
  1. public class A{
  2.  
  3. protected int x;
  4.  
  5. //missing code
  6. C obj = new C ();
  7. C.x=x; //i get an error "indentifier expected"
  8. }
  9.  
  10. class C extends B{
  11. protected int x;
  12. public int getInt()
  13. {
  14. System.out.print("Enter Value ");
  15. x=scan.nextInt();
  16. return x;
  17. }
  18. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 11
Reputation: ablago is an unknown quantity at this point 
Solved Threads: 1
ablago ablago is offline Offline
Newbie Poster

Re: Accessing variables of another class

 
0
  #2
Oct 27th, 2008
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.
Anderson Lago
Help me to write correctly
ablago@gmail.com
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 280
Reputation: joshmo is an unknown quantity at this point 
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

Re: Accessing variables of another class

 
0
  #3
Oct 27th, 2008
yep. that helped.. thanks
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC