Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Sunday, May 1, 2016

Access a class object from a method outside the main

Access a class object from a method outside the main


Sorry if the title isn't as accurate as it should be or maybe even falesly translated on my part. I'm german and a beginner in coding so take it easy one me. So I coded a programm in JAVA. It is a programm for a bank account that I picked up in a book and tried to expand. Now, to keep it simple in this thread I obviously reduced my example code on the problem.

public class Test1 {  public static void method(){      k.setNumber(300);  // This is where the problem is. From this method                          // I cannot acces the in main created class                          // object from 'Test'      }    public static void main(String[] args) {        Test k = new Test();        method();      k.getNumber();    }    }  

Now the class 'Test:

public class Test {    int number;    public int getNumber(){      return number;  }    public void setNumber(int nr){      number = nr;  }  }  

So is there any way around it? Otherwise I would have to write everything in the 'main', which is no problem in the case of this example, but in my original code thats not so easy to do.

Thanks for any help and advise. Hopefully this wasn't posted before because I already searched this site and the web.

Answer by Hbargujar for Access a class object from a method outside the main


you need to define

Test K = new Test()  

outside of either methods, i.e. main and method. Mostly you need to make 'K' global to be visible to both methods.

Answer by Brandon Laidig for Access a class object from a method outside the main


Currently, k only belongs to the main method. To access it from other methods within the same class, you need to make it a class

variable. This should be what you need:

public class Test1 {      private static Test k;        public static void method() {          k.setNumber(300);      }        public static void main(String[] args) {          k = new Test();          method();          k.getNumber();      }  }  

Answer by naveed for Access a class object from a method outside the main


you can do it as:

    public class Test1 {  public static void method(Test k){      k.setNumber(300);  // This is where the problem is. From this method                          // I cannot acces the in main created class                          // object from 'Test'      }    public static void main(String[] args) {        Test k = new Test();        method(k);//pass this test class instance to method      k.getNumber();    }    }  

Answer by Tom C for Access a class object from a method outside the main


You can either set k to be a global variable or you can pass it in the parameters of method like so.

public static void main( String[] argv )  {      final Test k = new Test();        method( k );      System.out.println(  k.getNumber() );  }    public static void method(Test k)  {      k.setNumber( 2 );  }  

Answer by Coralie B for Access a class object from a method outside the main


You can also use it as variable for your method :

public class Test1 {    public static void method(Test test){      test.setNumber(300);   }    public static void main(String[] args) {    Test k = new Test();      method(k);    k.getNumber();   }    }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

Related Posts:

0 comments:

Post a Comment

Popular Posts

Fun Page

Powered by Blogger.