Results 1 to 16 of 16
- 02-18-2009, 12:07 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 38
- Rep Power
- 0
Using an int's value in one class in a different class
Hello,
Im stuck with something relativly simple, but it is causing me so many problems.
Basically, I have two classes in one package. Lets say classA has an integer called int number = 2. How do I in totally different class called classB put system.out.println(number);.
It will bring up an error as "number" is not recognised in classB. How do I make this point to classA? I have tried to read through the java website but it is making me mighty confused.
Thank you
- 02-18-2009, 12:20 PM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
You either need to create an instance of class A in the method of Class B where you wish to access the variable or your class B should inherit class A, but in either class the variable should not be private.
e.g.
Java Code:class A{ int x=10; } class B{ void show(){ A a1=new A(); System.out.println(a1.x); } }
or
}Java Code:class A{ int x=10; } class B extends A{ void show(){ System.out.println(x); }
- 02-18-2009, 01:31 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 38
- Rep Power
- 0
Ok, I got that working in a small test enviroment, but when I apply it to my actual code it doesnt seem to work. Any ideas?
This is in one class, say class A
This is in the other, say ClassB.Java Code:public class ClassA{ public static void main(String[] args) { String text = "40 3 2 4 2 end"; Scanner s = new Scanner(text); double arg = 0; double tot = 0; while (s.hasNext() ) { if (s.hasNextDouble()) { double x = s.nextDouble(); arg += 1; tot += x; } if (s.hasNext("end")){ s.close(); }
I get an error saying "cannot resolve method println" and "cannot resolve symbol tot". I have no idea what is wrong.Java Code:public class ClassB{ public static void main(String[] args) { ClassA ca = new ClassA(); System.out.println(ca.tot); } }
- 02-18-2009, 01:33 PM #4
Its because you declared tot in a local variable inside the main method, not as field. Try declaring outside any methods.
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-18-2009, 01:42 PM #5
Member
- Join Date
- Feb 2009
- Posts
- 38
- Rep Power
- 0
Sorry to be a pain, I should really read up on my foundation java before tackling more indepth stuff. This is my first Object Oreintated language and I jumped in the deep end.
I need the main method otherwise for some reason, my scanner script won't work. How do I apply the tot variable outside of any "methods?"
Sorry.. I don't really understand the Java lingo at the moment, heh ^^ You have to spell it out for me even simpler.
- 02-18-2009, 01:49 PM #6
like this:
That shows local variables and fields. Note that the field is private, so it can only be used in the class, but then I provided a public accessor method which returns the field. You could just make the field public, but this way is better.Java Code:class Example { [COLOR="orange"]private[/COLOR] double inaccessablefieldbesidesthisclass = 7; [COLOR="Orange"]public[/COLOR] double getField() { return inaccessablefieldbesidesthisclass; } public static void main(String[] args) { double inaccessablelocalvariablebesidesthismethod = 10; } }
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-18-2009, 02:39 PM #7
Member
- Join Date
- Feb 2009
- Posts
- 38
- Rep Power
- 0
lol, I am so confused right now. I have also read several java tutorials on the web about classes and in my BIG JAVA book by Cay Horstman. I just dont understand any of this.
I know public class contains data and methods. I know a class can be instantiated by other other classes. I also know the different between public/private ect. I just don't know why it cant be simple to just take a value in a variable from one class and put it in another.
I triedBut i still got errors with the tot not being recognised. It would help if I knew what public double getField() did, just ive been trying to read up how these "contructors" work.. . even if they are constructors.. I have no idea.Java Code:public double getField(){ return tot; }
This is getting me really stressed :( I need a break
- 02-18-2009, 03:06 PM #8
I thought that was going to happen... OK,the quick and dirty solution is:
The above puts the tot variable within the scope of all the class. Use the code that you had before in your original class B and it should work.Java Code:public class ClassA{ [B][COLOR="Blue"]double tot = 0;[/COLOR][/B] public static void main(String[] args) { . . .
Now , what MK12 was trying to tell you was that the correct way to expose a variable (specially private type) to other classes is to use an accessor method ("accessor" comes from the word "access"). If you want to use it, you have to call the accessor method, not the variable:
Class A:
Class B:Java Code:public class ClassA{ double tot; public double [B][COLOR="Blue"]getTotal()[/COLOR][/B] { return tot; } public static void main(String[] args) { . . .
Hope this helped...Java Code:public class ClassB{ public static void main(String[] args) { ClassA ca = new ClassA(); System.out.println(ca.[B][COLOR="Blue"]getTtotal()[/COLOR][/B]); } }
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-18-2009, 03:37 PM #9
Member
- Join Date
- Feb 2009
- Posts
- 38
- Rep Power
- 0
Thank you for your help. I have got it working, but only to some extent.
With the changes:
Class A:
Java Code:public class ClassA{ double tot = 0; public static void main(String[] args) { String text = "40 3 2 4 2 end"; Scanner s = new Scanner(text); double arg = 0; double tot = 0; while (s.hasNext() ) { if (s.hasNextDouble()) { double x = s.nextDouble(); arg += 1; tot += x; } if (s.hasNext("end")){ s.close(); }
Class B:
This prints out 0.0, as the value of tot is intialised as 0. How do I print out the value of tot once it has gone through the while loop?Java Code:public class ClassB{ public static void main(String[] args) { ClassA ca = new ClassA(); System.out.println(ca.tot); } }
Edit: Just to let you know, it also does this if I use the other method suggested.
- 02-18-2009, 03:58 PM #10
Call class B from class A after tot has processed.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-18-2009, 04:09 PM #11
Member
- Join Date
- Feb 2009
- Posts
- 38
- Rep Power
- 0
How do I tell the program to do that?
This is what I have so far:
I think this should work, but the problem is its ClassB that is actually executed. I think for some reason, Its pulling the value of tot without actually making ClassA do anything to it.Java Code:public class ClassA{ double tot; public double getTotal() { return tot; } public static void main(String[] args) { String text = "40 3 2 4 2 end"; Scanner s = new Scanner(text); double arg = 0; ClassA ca = new ClassA(); while (s.hasNext() ) { if (s.hasNextDouble()) { double x = s.nextDouble(); arg += 1; ca.tot += x; } if (s.hasNext("end")){ s.close(); }Last edited by mainy; 02-18-2009 at 05:06 PM.
- 02-18-2009, 05:57 PM #12
Member
- Join Date
- Feb 2009
- Posts
- 38
- Rep Power
- 0
hello? sorry im a pain :( I am really trying to learn this stuff.
- 02-18-2009, 08:26 PM #13
Sorry... but I'm working right now. I would suggest the following link and I'll try to get back to you later:
Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-18-2009, 11:09 PM #14
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
It looks like to me you are trying to run two main methods, and although this is possible, this is not what you are looking for.
You should be able to reduce them so that there is only one, and the other should become a method.
Try pulling out the main method of class A and making it a public method that can be called on the instance of Class A in the main method of class B
- 02-18-2009, 11:33 PM #15
Member
- Join Date
- Feb 2009
- Posts
- 38
- Rep Power
- 0
Its ok, ive kind of given up on this project. It is due in tommorow, i did the most I could but I was stuck on a few of the questions, all of which required passing of information. Just a shame I couldn't get more marks.
I definatly think this is an area I need to look into though, I will make it my own task to try and master it.
Thank you for everybodys input.
- 02-19-2009, 11:53 PM #16
Senior Member
- Join Date
- Jul 2008
- Posts
- 125
- Rep Power
- 0
I thought I knew pain, then I read this thread.
The request is simple:
Demonstrate how a method in one object can
manipulate, or at least print a value in
another object.
To do this I'll use three classes,
A class named Arc, which has a launching
point, universaly known as:
and its two stooge classes, named A and B.Java Code:public static void main(String[] args){ .. }
The code, so far, is below. If you are working
with Windows, you can put all three classes in
one file, in this order, and it will compile.
But you want an object 'A' to print a 'number'Java Code:public class Arc{ public static void main(String[] args){ } } class A{ } class B{ }
in an object 'B'. So next, I'll add code for
that:
NOW IT WON'T COMPILE! The compiler tellsJava Code:public class Arc{ public static void main(String[] args){ } } class A{ public void aPrintB(){ System.out.println(number); } } class B{ int number = 102; }
us class A has been given the impossible
task of printing a value "number", which
does not exist in its scope.
I'll fix this by adding a constructor to
class A. A constructor specifies how I
want an object to be initialized, or
'instanciated'.
Upon instanciation I want object 'A' to
take a reference to an object 'B', and
to store it in a variable: "referenceToB"
..and still the same compiler error,Java Code:public class Arc{ public static void main(String[] args){ } } class A{ B referenceToB; // B reference A(B objTypeB){ // constructor referenceToB = objTypeB; // constructor } // constructor public void aPrintB(){ System.out.println(number); } } class B{ int number = 102; }
because object 'A' still does not know where
the hey to find "number".
I have corrected this below. Object 'A' will
print out a value "number", specifically
from an object of type B. This code will
compile.
Now run it using the command:Java Code:public class Arc{ public static void main(String[] args){ } } class A{ B referenceToB; // B reference A(B objTypeB){ // constructor referenceToB = objTypeB; // constructor } // constructor public void aPrintB(){ System.out.println(referenceToB.number); } } class B{ int number = 102; }
java Arc
(Remember, "Arc" contains the entry point
for this code, "public static void main( etc."
You'll get absolutely no results. Look at Arc,
it has absolutely no references to A or B!
It has no code at all, besides the launch
point!
So I'll have Arc 'instanciate' an "a" and "b"
object, and in it's main I'll have it prod
the "a" object for output from "b".
The following code will print the value of
"number" when you run Arc.
Here is similar code using static values.Java Code:public class Arc{ B b = new B(); A a = new A(b); public static void main(String[] args){ Arc arc = new Arc(); arc.a.aPrintB(); } } class A{ B referenceToB; // B reference A(B objTypeB){ // constructor referenceToB = objTypeB; // constructor } // constructor public void aPrintB(){ System.out.println(referenceToB.number); } } class B{ int number = 102; }
Java Code:public class Arc{ public static void main(String[] args){ B b = new B(); A a = new A(b); a.aPrintB(); } } class A{ B referenceToB; // B reference A(B objTypeB){ // constructor referenceToB = objTypeB; // constructor } // constructor public void aPrintB(){ System.out.println(referenceToB.number); } } class B{ int number = 102; }
Similar Threads
-
problem calling function from class to class
By alin_ms in forum New To JavaReplies: 3Last Post: 12-19-2008, 07:35 PM -
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 12:25 AM -
Able to find class file in WEB-INF/classes but not after add sub folders in class dir
By vitalstrike82 in forum Web FrameworksReplies: 0Last Post: 05-13-2008, 06:16 AM -
Class Reflection: Finding super class names
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:12 PM -
what is the Priority for execution of Interface class and a Abstract class
By Santoshbk in forum Advanced JavaReplies: 0Last Post: 04-02-2008, 07:04 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks