Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

member variable initialization  RSS feed

 
John Park
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, real basic question:


If you declare a member variable within a class, will the compiler generate an error if i don't initialize them? Another question: Do I need to declare my constructor parameters as member variables as shown below?

for example:

public class Car
{
private Date date;
private String make;
private String model;

public Car(Date date, String make, String model)
{
this.date = date;
this.make = make;
this.model = model;
}
public double someMethod()
{
...some code that doesn't use or initialize any of the above
member variables...
return 0;
}
}

Thanks in advance for the help,


JohnCP
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you don't have to explicity set the value of the member variables. If you don't assign them, they will be given default values when an object is created.

You do not have to create member variables which have the same name as the parameters of a constructor.
 
John Park
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help. You mentioned that the constructor doesn't have to pass in the same names as the member variables, so i'm assuming that memeber variables don't have to be created to be included as parameters in constructors...

Also, can you briefly summarize what the "this" keyword signifies? why do we need to declare that within the constructor?


Thanks,

JohnCP
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is used to differentiate class member variables(methods also) with method variables of the same name.

The arguments in the constructor are not the same as the class level variables. You could name them anything, and that way you don't have to use this, but I like to use the same names for both the constructor arguments and class variables. Once the constructor is completed, any arguments are no longer available, just like when any method terminates, the local variables are gone.

Although you can use this before any class variable, even if it doesn't explicitly need it, but that is frowned upon by many.

You don't have to pass something for each class variable, only when it is necessary.
 
stephen shields
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remember that with parameter passing, the parameter can be any name
they dont have to be the same name as your variables

i think it would be better advised tho to change the names of the parameters so there wont be any confusion if the variables are latter used and your variables are further changed.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!