• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the difference using a constructor here  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

still looking at constructors:

if i have this code...



i noticed that if i had int aircraftPassengers = 50; only in my constructor...i couldnt call it with my object say b747.aircraftPassengers.

but if i initialized the variable in the class...then added a value in the constructor it works...but what is the point of this? why use a constructor at all?
couldnt i just initialize all variables in the class? i dont understand the significance.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not really clear what you mean. You can initialize an instance variable in the constructor:

Or you can initialize it when you declare the variable:

Then if you later write

The output will be the same (50)  regardless of which of the above two ways you write your Aircraft class.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:i noticed that if i had int aircraftPassengers = 50; only in my constructor...i couldnt call it with my object say b747.aircraftPassengers. 
If I understood well, you mean when you declare and initialize aircraftPassengers only in constructor then why you can't access using object of class Aircraft? like this
Here int aircraftPassengers is a local variable whose scope is withing the constructor only, outside the body of constructor It ceases to exists, so when you try to access It in main method It might give you compile time error.  You can read here --->Local Variables

why use a constructor at all?
couldnt i just initialize all variables in the class? i dont understand the significance.
  • Declaraing variables and initializing in the class like this
  • is fine but sometime initializing variables might need some logic Or calculation which may need error handling or a loop in that case simple assignment is of no use. So you can initialize such instance variables in constructors, where error handling or other logic can be written.
  • Another reason could be keep declaration and initialization separate.
  •  
    Knute Snortum
    Sheriff
    Posts: 4081
    112
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    but if i initialized the variable in the class...then added a value in the constructor it works...but what is the point of this? why use a constructor at all?
    couldnt i just initialize all variables in the class? i dont understand the significance.

    What you often see with constructors setting instance variables is something like this:

    This allows the calling code to pass in 50 or 100 or 10 or whatever.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!