Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 second();
- }
- class bbb{
- aaa obj2=new aaa();
- System.out.println(obj2.value); //It gives error here also
- }
- // 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();
- }
- }
- class Aaa
- {
- //...
- public String getValue()
- {
- return this value;
- }
- }
- //...
- Aaa a = new Aaa();
- String test = a.getValue();
- //...
- class Foo
- {
- // ...
- public String value = "bar";
- //...
- }
- //...
- Aaa a = new Aaa();
- String test = a.value;
- //...
- // 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
- }
- }
- class aaa{
- static String value;
- public void method{
- value="as some output";
- }
- }
- class bbb extends aaa{
- aaa obj2=new aaa();
- obj2.method();
- System.out.println(value); //Output is "as some output";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement