Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,351 members, 7,808,229 topics. Date: Thursday, 25 April 2024 at 09:05 AM

Java Programmers - How Do I Pass Variable Value From A Classa To Another Classb - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Java Programmers - How Do I Pass Variable Value From A Classa To Another Classb (1146 Views)

Programmers, How Do You Cope With Generator Noise In Your Area? / Java Programmers Plz Help Out / Programmers how much do you earn monthly? (2) (3) (4)

(1) (Reply) (Go Down)

Java Programmers - How Do I Pass Variable Value From A Classa To Another Classb by ERCROSS(m): 9:39am On Mar 21, 2018
The program has three classes.
The main thread class(let's call it class0),
The second class collects input from users (Let's call it classA)
The third class (Let's call it classB) is meant to use the value input from classA in some of its program.

When I made classB extend classA, and I was able to use that particular classA variable within the classB. But I want to know if there is another way I can make that classA variable be used in the classB
Re: Java Programmers - How Do I Pass Variable Value From A Classa To Another Classb by CodeTemplar: 9:08pm On Mar 21, 2018
Create a fourth class and lets call that class classFour. Set up classFour in a way its fields can hold each of the variables you got from classA within classB
From within classB instantiate a new classC and pass the inputs you got from classA to classC like this ;

ClassFour cf = new ClassFour();
cf.setField1 = input1;
cf.setField2 = input2;
. . . And so on.

Then set classFour as a field within ClassC (cc) in our case.
ClassC cc = new ClassC();
cc.setCf(cf);



note: i had to type this on a opera mini 240 x 320 screen. It may not be neat on your side.

2 Likes

Re: Java Programmers - How Do I Pass Variable Value From A Classa To Another Classb by Yastec(m): 9:26am On Mar 22, 2018
Call the object of the class u want to use it variable and use dot notation to call the variable in the class u wish to use it. Note(make sure the variable is not declare private in the first class, if it is create a getter and setter method for each variable)

1 Like

Re: Java Programmers - How Do I Pass Variable Value From A Classa To Another Classb by ERCROSS(m): 1:12am On Mar 24, 2018
Guys, I've gotten a solution to the problem. What I did was that I went to the main thread, called the classC methods that are meant to convert the digit input to word. I ensure that these methods within classC has parameters type int (since the methods will be working an integer input) so that when I invoke them from the main class, I can pass them the value of the digit input as an argument. The identifier I used as the argument has been declared as an instance variable of the main thread, so the initialization occurred when I passed it the value of the digit input..

(1) (Reply)

I Need To Learn Programming, Anyone To Teach Me? / Help Me Learn The Basics / It Or Ict Related Course To Study

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 13
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.