java - Please explain gettersetter_method from this method -


i have 1 small program related getter , setter concept, i'm getting null value reason don't understand. here code, need pass multiple strings in single object:

public class simple {      public object getmethod(){         getter s=new getter();         string app="apple";         s.setapp(app);               string ble="blue";         s.setgrp(ble);         string re="grape";         s.setgrp(re);                    return s;     } } 

here getter method:

public class getter {     string app;     string org;     string  grp;     string mn;      public string getapp() {         return app;     }      public void setapp(string app) {         this.app = app;     }      public string getorg() {         return org;     }      public void setorg(string org) {         this.org = org;     }      public string getgrp() {         return grp;     }      public void setgrp(string grp) {         this.grp = grp;     } } 

and here test class:

public class check {      public static void main(string[] args) {         simple s=new simple();         getter g=new getter();         s.getmethod();         string first=g.getapp();         string second=  g.getgrp();         string third=g.getorg();         system.out.println("the value 1st " + first + "the second" +                                second +"3rd is"+third);     }     } 

this getting null value.

here getting null value

you printing new getter variables, have not been set, , null.

on own, s.getmethod() call method, , create new getter (different 1 made earlier), return nothing, need assignment.

getter g=(getter)s.getmethod();  

if don't want cast, change method return getter instead of object

public getter getmethod(){ 

so can

getter g=s.getmethod();  

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -