Use a variable returned in a getter method in another class

General Tech Learning Aids/Tools 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I am still learning but seem to fall at what should be very simple hurdles; my strong points seem to be with the logic of equations but I possess little skill at remembering and implementing functions and correct simple syntax's.

The class that I am implementing the method in is MyClass, the method that I want to define the variable taken from the other class is establishIrEvent. The class that I want to collect the variable from is IREvent, the getter method that returns the variable in this class that I want to collect is getX.

Now for the code:

Getter method from IREvent class:

public int getX() {
    return x;
}

Method that I want to use that variable in to assign, (with my terribly poor attempt) in MyClass:

public void establishIrEvent(IREvent arg0) {

    int source = (IREvent)arg0.getX();

}

Any advice will be hugely appreciated and fingers crossed this question may aid someone else in a similar pickle! Please ask for any more info as I always seem to miss something or ask a question that annoys an experienced developer somewhat.

profilepic.png
manpreet 2 years ago

There are a couple of problems with your code. First, you are trying to assign an int to a Stringbecause getX returns int and the source variable is a String. if you want to do this, then try :

String source = Integer.toString(arg0.getX());

Second, and less of an issue, there is no need to cast arg0 to IREvent as it's already defined as that in the establishIrEvent method.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.