| | |
Accessing variables of another class
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 |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows





