AppTime
AppTime

Reputation: 37

Java, is this a declaration?

public void sendMessage(View view){
//todo
}

Is View view declaring (giving a name ( "view" ) to?) class View? So it can be initialized later (view = new View();)?

Upvotes: 2

Views: 76

Answers (3)

StackFlowed
StackFlowed

Reputation: 6816

No this means when the function is called view will be passed as an parameter.

Eg:

public int sum (int num1, int num2){
    return num1+num2;
}

Now you would call this like:

System.out.printn(sum(1,2));

This would print 3.

In your case where would call the function sendMessage. You would do it like

View view= new view();
sendMessage(view);

EDIT:

public void sendMessage(View view){
    // Here you can use view as if it is already set you don't need to create a new view.
   // Example you might want some attributes from view to send message.
   System.out.println("View name" + view.getName());
   System.out.println("View id" + view.getId());
   // Note this is just an example. you can use attributes from view in here.
}

Upvotes: 3

Paul92
Paul92

Reputation: 9062

view is a parameter of the method sendMessage. Technically, it is creating a new variable, called view, of type View that has a special ability: it is initialized with the value you pass when calling the sendMessage method.

Upvotes: 1

John Park
John Park

Reputation: 290

"View" is your data type. "view" is your variable. It looks like you are passing the "view" as a parameter to a function to be used locally within that function.

view = new View() can be initialized within that function as long as "view" is globally declared.

Upvotes: 1

Related Questions