Can't call a method from a separate class in Java -
making little mini blog application, setting places hold data. have 3 classes 1 post 1 user , 1 test, named blog. when try call getname(); method in blog class won't run keeps saying needs string, made array of user objects, , input string username spot, , still isn't working.
public class blog { public static void main(string []args) { user[] userlist = new user[3]; userlist[0] = new user("what.com", "thelegioncrab", "jake parham", "jparham@gmail.com"); userlist[1] = new user("huh.com", "ragglefraggle", "brett hawkins", "bhawkins@gmail.com"); userlist[2] = new user("buh.com", "seanbeast", "sean sweeney", "ssweeney@gmail.com"); for(int counter = 0; counter<userlist.length; counter++) { user.getname(); } } } public class user { private string url; private string username; private string realname; private string email; public user(string url, string username, string realname, string email) { this.url = url; this.username = username; this.realname = realname; this.email = email; } public void getname(string username) { system.out.println(username); } }
public void getname(string username) { system.out.println(username); }
here function require string. why need string run. if want print username of current user object in loop use this.
public void getname() { system.out.println(this.username); }
this refer current user object in loop. loop.
for(int counter = 0; counter<userlist.length; counter++) { user.getname(); }
you use user class meanwhile create variable as
user[] userlist = new user[3];
to print var, should use var.
for(int counter = 0; counter<userlist.length; counter++) { userlist[counter].getname(); }
Comments
Post a Comment