Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Variable that equals class?
I have a class called SampleClass. I want to call a variable from this class. The variable I want is called VariableOne. I can simply type:
SampleClass.VariableOne;
But i really need to access the specific class by using a variable. Is there any way I make a variable like... Class TempClass = SampleClass;
In order to go,
TempClass.VariableOne;
3 Answers
- AnalProgrammerLv 78 years agoFavorite Answer
In the first instance it all depends on the language you are using.
Then it depends on the access specifiers as to how you access variables within the class.
In general all variables within a class should be private.
You should then have methods or functions in the class that can get or set the values of the variables as well as the constructor to give the variables initial values.
So now you get the data you want by using
SampleClass sc = new SampleClass();
int data = sc.getVariable();
Have fun.
- 8 years ago
I'm not sure what language you're using, but I'm pretty sure that in order to access data in a class, you must instantiate that class. Simply put, you must create an object in order to access the data within. Without doing this, I don't think you can access data in that class.
- 8 years ago
To make a new Object you should call
ClassName name = new ClassName("parameters_if_any");
In this example shows the correct way:
//ExampleClass (our object...)
public class ExampleClass{
private String parameter;
ExampleClass(String parametergotten){
this.parameter = parametergotten;
}
public String getParameter(){
return this.parameter;
}
public void printParameter(){
System.out.println(this.parameter);
}
}
//our main class Main
public class Main{
public static void main(String[] args){
ExampleClass example = new ExampleClass("asdf");
String par = example.getParameter();
example.printParameter();
System.out.println(par);
}
}
This would be an excellent example. Please ask questions via email if confused or need help!
I will be more than glad to help.
Reguarding what you said I cannot follow what you said.
- http://answers.yahoo.com/ Freelance developer question answerer
- Souly helping to follow Jesus and help novice coders!
- I teach Java, HTML, CSS, JavaScript (Email me I teach all)