| | |
problems accessing class instances
![]() |
•
•
Join Date: Nov 2004
Posts: 15
Reputation:
Solved Threads: 0
I'm having a problem whereby I create an instance of a class called Calculations.class in one of my main classes, but cannot access that instance from any other class. Here is the code
public class Parameters{
//code//
Calculations calc = new Calculations();
//some calculations done using variables in the calculations class and new data is saved//
//end of Parameters.class//
}
public class Intake{
// I now want to work with the instance "calc" created above so I can use the variables that were set in Parameters.class, but I don't know how to reference this instance!! //
Any help will be greatly appreciated, thank you!!
public class Parameters{
//code//
Calculations calc = new Calculations();
//some calculations done using variables in the calculations class and new data is saved//
//end of Parameters.class//
}
public class Intake{
// I now want to work with the instance "calc" created above so I can use the variables that were set in Parameters.class, but I don't know how to reference this instance!! //
Any help will be greatly appreciated, thank you!!
You could make a method in your parameters class called getCalculator, which simply returns the calculator -
public Calculator getCalculator()
{
return calc;
}
Then create an instance of parameters in intake and call the getCalculator method upon it.
Parameters pram = new Parameters();
Calculators calc2 = new Calculators();
calc2 = pram.getCalculator();
Hopefully that will help.
public Calculator getCalculator()
{
return calc;
}
Then create an instance of parameters in intake and call the getCalculator method upon it.
Parameters pram = new Parameters();
Calculators calc2 = new Calculators();
calc2 = pram.getCalculator();
Hopefully that will help.
•
•
Join Date: Nov 2004
Posts: 15
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by MrScruff
You could make a method in your parameters class called getCalculator, which simply returns the calculator -
public Calculator getCalculator()
{
return calc;
}
Then create an instance of parameters in intake and call the getCalculator method upon it.
Parameters pram = new Parameters();
Calculators calc2 = new Calculators();
calc2 = pram.getCalculator();
Hopefully that will help.
Intake.java [189:1] <identifier> expected
calc = pram.getCalculator();
^
1 error
Errors compiling Intake.
it would have to be the calculator created in parameters so
public Calculator getCalculator()
{
Calculator calc1;
calc1 = calc;
return calc1;
}
hows that? but then if its been declared public at the top you should just be able to return calc.
what you reckon?
public Calculator getCalculator()
{
Calculator calc1;
calc1 = calc;
return calc1;
}
hows that? but then if its been declared public at the top you should just be able to return calc.
what you reckon? •
•
Join Date: Sep 2004
Posts: 84
Reputation:
Solved Threads: 1
I think what you have is an issue of scope.
Looking at the above, I think you placed the getCalculator method in the wrong class. It should go in the Parameters Class.
So you would have the following:
Looking at the above, I think you placed the getCalculator method in the wrong class. It should go in the Parameters Class.
So you would have the following:
Java Syntax (Toggle Plain Text)
public class Parameters{ Calculations calc = new Calculations(); //some calculations done using variables in the calculations class //and new data is saved public Calculations getCalculator() { return calc; } } //end of Parameters.class public class Intake{ // I now want to work with the instance "calc" created above so I can // use the variables that were set in Parameters.class, but I don't know // how to reference this instance!! Parameters pram = new Parameters(); Calculators calc2 = new Calculators(); calc2 = pram.getCalculator(); }
•
•
Join Date: Nov 2004
Posts: 15
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by jerbo
I think what you have is an issue of scope.
Looking at the above, I think you placed the getCalculator method in the wrong class. It should go in the Parameters Class.
So you would have the following:
Java Syntax (Toggle Plain Text)
public class Parameters{ Calculations calc = new Calculations(); //some calculations done using variables in the calculations class //and new data is saved public Calculations getCalculator() { return calc; } } //end of Parameters.class public class Intake{ // I now want to work with the instance "calc" created above so I can // use the variables that were set in Parameters.class, but I don't know // how to reference this instance!! Parameters pram = new Parameters(); Calculators calc2 = new Calculators(); calc2 = pram.getCalculator(); }
•
•
Join Date: Sep 2004
Posts: 84
Reputation:
Solved Threads: 1
Opps, I mispelled, Calculations
The second class should be:
Also I suspect you have Calculations defined somewhere?
The second class should be:
public class Intake{
// I now want to work with the instance "calc" created above so I can
// use the variables that were set in Parameters.class, but I don't know
// how to reference this instance!!
Parameters pram = new Parameters();
Calculations calc2 = new Calculations();
calc2 = pram.getCalculator();
}Also I suspect you have Calculations defined somewhere?
•
•
Join Date: Nov 2004
Posts: 15
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by jerbo
Opps, I mispelled, Calculations
The second class should be:
public class Intake{ // I now want to work with the instance "calc" created above so I can // use the variables that were set in Parameters.class, but I don't know // how to reference this instance!! Parameters pram = new Parameters(); Calculations calc2 = new Calculations(); calc2 = pram.getCalculator(); }
Also I suspect you have Calculations defined somewhere?
calc2 = pram.getCalculator();
What do I need to do now?
•
•
Join Date: Sep 2004
Posts: 84
Reputation:
Solved Threads: 1
Actually you dont even need to assign (I was getting an error too.) This does compile:
Java Syntax (Toggle Plain Text)
public class Intake{ // I now want to work with the instance "calc" created above so I can // use the variables that were set in Parameters.class, but I don't know // how to reference this instance!! Parameters pram = new Parameters(); Calculations calc2 = pram.getCalculator(); // or you could even say (since calc is public) Calculations calc3 = pram.calc; }
![]() |
Other Threads in the Java Forum
- Previous Thread: How to make Java and C++ sockets play nice
- Next Thread: IO and Embedded SQL
| Thread Tools | Search this Thread |
911 actionlistener addball addressbook android api applet application array automation binary block bluetooth button character class client code component consumer css csv database desktop developmenthelp eclipse ee error fractal ftp game gameprogramming givemetehcodez graphics gui html ide image j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia jvm lego linked linux loan mac map method mobile netbeans notdisplaying number objects online oriented phone printf problem program programming project projects recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server service set singleton sms software sort sql swing system test textfields threads time title tree tutorial-sample ubuntu update windows working





