how to use the variable of one class into another class using java?
Ihave two class files. In that first file, i stored aaa class firl and in the next text file i store the class file bbb.Io want to use the variable of aaa class into bbb class. How to use use it.
Note :If i put String value variable as public then it shows some error.
class aaa{
public void method{
String value="as some output";
string other="it has some output";
}
}
public static void main(String args[]){
aaa obj=new first();
bbb object=new seco开发者_JAVA百科nd();
}
class bbb{
aaa obj2=new aaa();
System.out.println(obj2.value); //It gives error here also
}
Please suggest ideas. Thanks in advance.
You are missing some basic knowledge of Java, maybe read up a few tutorials on the web.
// use capitals for the first letter, conventions are good ;)
public class Aaa{
// I think this is what you meant, you want member variables, add public if you
// want them to be accessible anywhere
public String value="as some output";
public String other="it has some output";
// missing brackets
public void method() {
// do something/anything
}
}
public class Bbb{
// you need to put this in a main... without getting exceptions
public static void main(String args[]){
Aaa obj2=new Aaa();
// now you can access the field value, since it's public
System.out.println(obj2.value); //error should be gone now
}
}
public class TestMain{
public static void main(String args[]){
// first and second aren't classes, you meant Aaa and Bbb?
Aaa objA=new Aaa();
Bbb objB=new Bbb();
}
}
Your class aaa does not have a public member variable that is called value. You have a local variable value in your method, which you will not be able to use.
There are mainly two options:
a) Use a getter method.
class Aaa
{
//...
public String getValue()
{
return this value;
}
}
//...
Aaa a = new Aaa();
String test = a.getValue();
//...
b) Use a public member variable.
class Foo
{
// ...
public String value = "bar";
//...
}
//...
Aaa a = new Aaa();
String test = a.value;
//...
I recommend you use the first one.
To answer your question, value is a local variable within the Bbb.method
method. To access it from another class, it must be an instance variable of the class (declared in the class but outside any method) and accessible (public, or package (aka default), or private with getter/setter methods)
// note that I've renamed classes to follow the convention of uppercasing class names.
// this makes the code much easier to read.
class Aaa {
public String value = "instance value initialized when the class loads (first use)";
public String other = null;
// a method declaration must have parentheses, even if it takes no parameters
public void method() {
other = "instance value, initially null, set by calling method";
}
}
class Bbb {
Aaa aaaInBbb = new Aaa();
public void method(){
// every statement (except variable declarations) must be in a method
System.out.println(aaaInBbb.value); // access the public value in aaaInBbb
}
}
class C {
// note that main(...) must be in a class as well - as all methods in Java must
public static void main(String[] args) { // convention also puts [] next to the type
Aaa aaa = new Aaa(); // this variable is never used.
Bbb bbb = new Bbb();
bbb.method(); // causes instance of Bbb to print out aaaInBbb.value
}
}
I've added some additional comments on syntax and standard code conventions that will help you as you learn Java.
You can simple declare the variable and initialize the value of variable in method1 and finally extends that class.
class aaa{
String value;
public void method(){
value="as some output";
}
}
class bbb extends aaa{
public void method2(){
System.out.println(value); //Output is "as some output";
}
public static void main(String as[]){
bbb obj2=new bbb();
obj2.method(); //getting the value of string value
obj2.method2(); //print the value in method2
}
}
精彩评论