How to use a class argument in another class in Java -


first mention beginner programmer , trying learn. in short problem looks this: have 3 classes (main, author, book)

public static void main(string[] args) {     author a=new author();     book b=new book();     //creating author     a.setname("paulo coelho", "pc@gmail.com");     system.out.println("author : " + a.getname() + "\ne-mail: "+ a.getemail());     //creating book     b.setname("alchimist");     b.setyear(2010);     b.setprice(15.7);  } public class author {     public static string name;     public static string email;     public static void setname(string n, string e){         name=n;         email=e;     }     public string getname(){         return name;     }     public string getemail(){         return email;     }   public class book {     string name;     int year;     double price;     author a;       public void setname(string b){         name=b;     }     public void setyear(int y){         year=y;     }     public void setprice(double p){         price=p;     }      public string getname(){         return name;     }     public int getyear(){         return year;     }     public double getprice(){         return price;     }     public string getbookdetails(){         ?????????????     } } 

how can make connection between author , book when set book details linked first created author , when print getdetails details right book ?

you have property author a; in book class.

you need add setter the authos in book class can set main method:

 // in book class  public void setauthor(author author){       this.a = author;   } 

 // in main:  b.setauthor(a);  b.setname("alchimist"); 

after can access properties of assigned author in method getbookdetails().


btw

in java length of identifier names names virtually unlimited. there no penalty in way long identifier names. don't stingy letters when choosing names. eg. in example use always author instead of a , book instead of b. make life easier in long run...


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -