many methods and instances help?

-1 khris microbean · September 6, 2014
At first I was very confused and almost gave up. But after re-watching the vid like 20 times I think I almost got it. Two questions tho. 1) what does return do? (return girlName; ) does it just put girlName's value in getName?   and 2) Why use setName(String name) when temp becomes = to girlname at the end? wouldn't setName(String temp) work?

Post a Reply

Replies

Oldest  Newest  Rating
+3 Saurabh patel · September 7, 2014
here,
private girlName;
public setName(String name)
{
   girlName=name;
}





we use this setName method with parameter name in it and also gave it's value to the girlName variable which we cant  directly access other  class because  it is private variable only access in that class  ..
 
it works if u pass "name" as parameter  in when u calling the class tuna method in class apples by making it's object in it ....    
String name=input.nextLine();
tunaObject.setName(String name);

so it gives value of name to the girlName, which is indirectly given to girlName.
 
I recommand you to make changes practically and see what happens, you'll understand more .. :)
thank you bucky sir.
+1 Pere Garau Burguera · September 7, 2014
When a method returns something it means it gives that value. For example this method

public int method(){
return 0;
}

always returns 0, and this means that whenever you call it, it's gonna be equal to 0, so

x = method();
System.out.println(x);

it's gonna print out the return value of the method, which in this case is 0. It's like the method itself is equal to whatever it returns, and therefore you can use it as if it was a variable.
  • 1

Java / Android Development

107,205 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator