Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

trying to get a value from another class  RSS feed

 
Karis Brown
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Karis Brown
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks davy!! i will try it out
 
Karis Brown
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BUMP!
 
john smith
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kinda, I'm VERY new to this Java so an example would be nice please!
 
Karis Brown
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well done Karis,
Davy
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!