-
Java (
http://www.daniweb.com/forums/forum9.html)
| matt_5104 | Nov 22nd, 2004 8:31 am | |
| problems accessing class instances 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!! |
| MrScruff | Nov 22nd, 2004 8:56 am | |
| Re: problems accessing class instances 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. :) |
| matt_5104 | Nov 22nd, 2004 9:15 am | |
| Re: problems accessing class instances Quote: 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. :) | Hi, it almost helps, but when I try and run the program, it comes up with:
Intake.java [189:1] <identifier> expected
calc = pram.getCalculator();
^
1 error
Errors compiling Intake. |
| jerbo | Nov 22nd, 2004 9:39 am | |
| Re: problems accessing class instances public Calculator getCalculator()
{
return calc;
}
You never declare what 'calc' is.
public Calculator getCalculator()
{
Calculator calc;
return calc;
}
However I would assume that you would assign something to 'calc'? |
| MrScruff | Nov 22nd, 2004 9:48 am | |
| Re: problems accessing class instances 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. :S what you reckon? |
| jerbo | Nov 22nd, 2004 10:55 am | |
| Re: problems accessing class instances 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:
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();
} |
| matt_5104 | Nov 22nd, 2004 1:40 pm | |
| Re: problems accessing class instances Quote: 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:
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();
} | Still doesn't work, even with the modifications given above. I still work on it though, must be some way round it!! |
| jerbo | Nov 22nd, 2004 4:05 pm | |
| Re: problems accessing class instances 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? |
| matt_5104 | Nov 22nd, 2004 4:12 pm | |
| Re: problems accessing class instances Quote: 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? | Yes, calculations is defined elsewhere. However, when I add the code you provided me with(very grateful!!), the compiler returns with an error saying that an identifier is missing before the line of code:
calc2 = pram.getCalculator();
What do I need to do now? |
| jerbo | Nov 22nd, 2004 4:12 pm | |
| Re: problems accessing class instances Actually you dont even need to assign (I was getting an error too.) This does compile:
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;
} |
| All times are GMT -4. The time now is 11:36 am. | |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC