| | |
Accessing variables of another class
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2007
Posts: 280
Reputation:
Solved Threads: 19
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.
Java Syntax (Toggle Plain Text)
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; } }
•
•
Join Date: Sep 2008
Posts: 11
Reputation:
Solved Threads: 1
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.
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.
![]() |
Similar Threads
- Accessing a method from class 1 in class 3 (C++)
- How do I access variables in my queue? (C++)
- problems accessing class instances (Java)
- accessing private data members (C++)
Other Threads in the Java Forum
- Previous Thread: StringUtil Help(AP CS Work Urgent help please!)
- Next Thread: <identifier> expected? Pls help my code!
| Thread Tools | Search this Thread |
Tag cloud for Java
actionlistener android api apple applet application apps arguments array arrays automation balls binary bluetooth card chat class classes clear client code component consumer database draw eclipse ee error event exception fractal free game gameprogramming gis givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javaprojects jni jpanel julia jvm key linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie nextline nls notdisplaying number oracle output print problem program programming project recursion recursive scanner screen security server set size sms socket sort spamblocker sql sqlite string sun swing terminal test threads time tree trolltech windows





