• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

member variable initialization

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic