Results 1 to 16 of 16
- 04-09-2012, 10:19 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
Q>Constructors! how to pass a value from MainClass to SubClass and back to MainClass?
i want to know about constructors..
i have a value from MainClass which is "a=1",
i want to pass it to SubClass and add "a=a+5" and print tha "a" value in SubClass,
and last the new value of "a" in SubClass which is "6" and pass again to the MainClass
and change the original value of MainClass "a=1" to new value of "a=6" from SubClass..
Java Code:class MainClass { public static void main(String[] args) { SubClass SubClassObject = new SubClass(); int a = 1; SubClassObject.putNumber(a); System.out.println("the value of a in MainClass :" + a); } }
original output:Java Code:public class SubClass { public void putNumber(int a) { a = a + 5; System.out.println("the value of a in SubClass :" + a); } }
the value of a in SubClass :6
the value of a in MainClass :1
i want this output:
the value of a in SubClass :6
the value of a in MainClass :6Last edited by boblingwide; 04-09-2012 at 10:24 AM. Reason: speling
- 04-09-2012, 10:59 AM #2
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
Change the putNumber(...) method to return the incremented value, and assign that return value to a in MainClass.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-09-2012, 11:00 AM #3
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
Your example doesn't make a lot of sense. Your example mutes the power of inheritance. First your subclass must extend your Main class using the extends key word.
your subclass will have access to any instance variables that are public or protected in your MainClass (super Class)Java Code:public class SubClass extends MainClass { }
I think what you are trying to accomplish would make sense only like this:
then:Java Code:public class MainClass { protected int a = 0; //constructor publicMainClass() { a = 1; } }
Remeber Java allows you to treat an object of a sub class as an object of its super class (polymorphism).Java Code:public class SubClass extends MainClass { //Constructor public SubClass() { super.a += 5; } }
That's why your example is a little on the odd side.
hope this helps!
- 04-09-2012, 11:52 AM #4
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
- 04-09-2012, 11:56 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
mwr1976.. ahh u can use extends also hmm tnx!Remeber Java allows you to treat an object of a sub class as an object of its super class (polymorphism).
That's why your example is a little on the odd side.
hope this helps!
can u aply also without using "extends" like ordinary codes like in my SubClass adding only small codes to get the same output without using extends?
- 04-09-2012, 01:13 PM #6
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
You use the term "sub class". This means that it is a derived class. In order for a class to be a "sub class" (derived class) you have to extend your "base class" It might be a little easier to help you if you could explain why you are trying to do this.
- 04-09-2012, 05:10 PM #7
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
ahm just imagine subclass is not the subclas of mainclass, imagine its just another javafile want to send a value and to call it back with a new value..
ahm is there other way to return the value a of subclass "a" to back to mainclass "a"?
plz help me im still a nobie in java..plz!
- 04-09-2012, 05:24 PM #8
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
Use the return statement in the method with the value you want returned to the caller of the method:way to return the value a
return a;If you don't understand my response, don't ignore it, ask a question.
- 04-09-2012, 05:28 PM #9
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
There is no way to do this. The variable "a" only has scope within the class that it is created in. If you want to work with that exact variable your only choice is to use inheritance(extending the base class). The only other thing you can do(which has nothing to do with constructors) is to create a method that adds 5 to whatever parameter is passed to it and have it return the result to the caller.Lets call the method "AddTo()". you would have to to create a subclass instance and then call the AddTo() method."i have a value from MainClass which is "a=1",
i want to pass it to SubClass and add "a=a+5" and print tha "a" value in SubClass,
and last the new value of "a" in SubClass which is "6" and pass again to the MainClass
and change the original value of MainClass "a=1" to new value of "a=6" from SubClass.."
it would be something like:
Java Code:public int AddTo(int num) { return num + 5; }
hope it helps!Java Code:SubClassInstance.AddTo(a);
- 04-09-2012, 05:31 PM #10
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
You would want to assign the value returned to a variable (for example: a) otherwise it will be lost.
If you don't understand my response, don't ignore it, ask a question.
- 04-09-2012, 07:17 PM #11
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
- 04-09-2012, 07:20 PM #12
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
where exact line im going to insert this codes in my SubClass?
Java Code:public int AddTo(int num) { return num + 5; }
And this codes where should i put it in my MainClass or SubClass?
hope it helps![/QUOTE]Java Code:SubClassInstance.AddTo(a);
- 04-09-2012, 07:26 PM #13
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
line 3 returns a value from the addTo() method.
To receive the returned value, assign what is returned by the method in a variable:
a = subClassInstance.addTo(a); // pass a to method and save the returned value back into aIf you don't understand my response, don't ignore it, ask a question.
- 04-10-2012, 11:13 AM #14
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
norms i tried it in line 3 and eror ahm can u help me edit my entire main and subclas?
Last edited by boblingwide; 04-10-2012 at 11:16 AM. Reason: speling
- 04-10-2012, 11:15 AM #15
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
Re: Q>Constructors! how to pass a value from MainClass to SubClass and back to MainCl
ahm can u edit my codes i cant get it right plz norm?
i cant get it right plz..
Java Code:class MainClass { public static void main(String[] args) { SubClass SubClassObject = new SubClass(); int a = 1; SubClassObject.putNumber(a); System.out.println("the value of a in MainClass :" + a); } }Java Code:public class SubClass { public void putNumber(int a) { a = a + 5; System.out.println("the value of a in SubClass :" + a); } }
- 04-10-2012, 01:03 PM #16
Similar Threads
-
pass data from Jdialog back to jFrame
By Rogue45 in forum AWT / SwingReplies: 4Last Post: 04-05-2012, 12:35 AM -
Scriptlet Not Running on JSP after coming back from back button of browser
By jason.3dmagic in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 06-23-2011, 07:44 AM -
How to pass variable back from thread for animation
By blue50 in forum AWT / SwingReplies: 0Last Post: 12-02-2010, 04:54 AM -
Subclass help
By amystauff in forum New To JavaReplies: 1Last Post: 05-30-2010, 04:36 AM -
how to pass an arraylist from an object back to the parent object that it was created
By george_a in forum New To JavaReplies: 1Last Post: 03-04-2009, 06:14 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks