File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes trying to get a value from another class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "trying to get a value from another class" Watch "trying to get a value from another class" New topic
Author

trying to get a value from another class

Karis Brown
Ranch Hand

Joined: Nov 13, 2003
Posts: 33
ok, erm..
in one class I have calculated a value 'total' now I want it to add to another total in another class that extends the same class. how do i take that value and add it to another in another class?


So Cold & So Outa Control
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Hi Karis, welcome to the ranch.

Hope this helps to get your value from one class to another class.
Davy
[ March 03, 2004: Message edited by: Davy Kelly ]

How simple does it have to be???
Karis Brown
Ranch Hand

Joined: Nov 13, 2003
Posts: 33
thanks davy!! i will try it out
Karis Brown
Ranch Hand

Joined: Nov 13, 2003
Posts: 33
i do that but it inherits all the controls from the previous panel doubling up on checkboxes! is there a way to avoid this?
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Karis,
Find the variable, that you want to get (total which you were chatting about), you could make it private in the class that it is in, then make an object of that class to access the variable that you want.
I have ammended the code to include a private variable and a get method, so that you can access the variable.

e.g.

does this help?
Davy
[ March 03, 2004: Message edited by: Davy Kelly ]
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
Give us more detail...it's rare to have a subclass contain a reference to an instance of a super class and be accessing its data in the way you describe. Perhaps your design could be improved and this problem will solve itself.
sev
Karis Brown
Ranch Hand

Joined: Nov 13, 2003
Posts: 33
thanks even though looking at this i am completely lost, but i'm sure i will get the knack of it eventually.
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Karis,
Post some of your code and someone will see what is going on.
use the code button below the Add Reply button and place you code between the tags, this way it keeps indentation.
no doubt you will get a response which is more accurate to your neeeds.
Davy
Karis Brown
Ranch Hand

Joined: Nov 13, 2003
Posts: 33

HELP! i get
---------- Java Complier ----------
A2PizzaProgram.java:115: non-static method getTotal() cannot be referenced from a static context
double total = pSc.getTotal();
^
1 error
Normal Termination
Output completed (0 sec consumed).
[ March 08, 2004: Message edited by: Karis Brown ]
Karis Brown
Ranch Hand

Joined: Nov 13, 2003
Posts: 33
BUMP!
john smith
Ranch Hand

Joined: Mar 04, 2004
Posts: 75
You can't use non-static methods in static context. What do I mean by that? Well:
class ClassA {
public String aMethod() {
return "Hello";
}

public static String anotherMethod() {
return "Hello Again";
}
}
class ClassB {
// legal
ClassA instanceOfClassA = new ClassA();
String helloString = instanceOfClassA.aMethod();
// illegal
String helloString = ClassA.aMethod();
// legal
String halloAgainString = ClassA.anotherMethod();
}
Why? Unless a method is declared static then it is only accessable through an instance of that class. If you want to use your getTotal() method, either declare it static, or create an instance of pSc and call it on that. Does this make sense?
Karis Brown
Ranch Hand

Joined: Nov 13, 2003
Posts: 33
Kinda, I'm VERY new to this Java so an example would be nice please!
Karis Brown
Ranch Hand

Joined: Nov 13, 2003
Posts: 33
LALALALLALALA! HAHAH!! :LOL! LMAO!! THANK YOU VERY MUCH!!!
U GUYS ARE GREAT!!!
Its working now!!
Thanks again, thanks!!
[ March 09, 2004: Message edited by: Karis Brown ]
[ March 09, 2004: Message edited by: Karis Brown ]
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
well done Karis,
Davy
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: trying to get a value from another class
 
Similar Threads
Program for retirement funds..(STUCK)
Need Quick Answers...!
Percision problems with Double and Float numbers
sentinal controlled looop
Anagrams