This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin Cookbook and have Ken Kousen on-line!
See this thread for details.
Win a copy of Kotlin Cookbook this week in the Kotlin forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Constructor and the Instance Variable

 
Ranch Hand
Posts: 87
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you declare/define a Constructor with initial values, do you need Instance variables or can you, or do you, use both Instance variable and assign variables initial values via the Constructor(s) in Java programs?
 
Marshal
Posts: 67050
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure I understand the question, but I suspect the answer is yes to all parts. Of course, if you fail to initialise a field, it will have a default value, which might be null, and that can cause problems.
In my opinion, you should initialise every field to a sensible value that establishes the class' invariants in every constructor.
 
Ranch Foreman
Posts: 240
5
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instance variables in the constructor allow you to take arguments. this lets you assign the correct values at the same time that you create the object.
otherwise you would need to first create an object with null values or placeholder values, and then set the correct values at the time of your choosing.

you can do that with a setter method like this:
myCar.setMake("Ford");

or by directly accessing the variable if it's public, like this:
myCar.make = "Ford";

here's a horrible looking code example i just made, the forum doesn't let me tab-indent
 
Campbell Ritchie
Marshal
Posts: 67050
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

S Fox wrote:. . . the forum doesn't let me tab-indent . . .

The problem with the tab character may be because websites assume it means, “move focus to the next object.” If you want tab‑indentation, copy'n'paste it from your text editor, but most of us prefer to use multiple spaces than tabs.
 
Royale Summers
Ranch Hand
Posts: 87
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me re-state the question.

In Production, would a program use Instance Variables or just initialize variables via the Constructor or would a live program utilize both Instance Variables and initialize variables using Constructors?

I'm wondering if it's an either or situation or do programs use both approaches.

Hope this clarifies.

Thanks.
 
Sheriff
Posts: 14622
243
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do what makes sense semantically.

A constructor that takes no arguments means either the class is stateless (no instance variables) or has sensible defaults for its initial state.

A constructor that takes arguments for attributes that are backed by instance variables that do not have setters can mean a few things:

If the instance variables are final, then that means they can only be set via a constructor and that the values cannot change during the life of that object.

If the instance variables are not final, then they are probably going to be mutated as a consequence of invoking some kind of behavior on an object. For example, a Car object might have a mileage that is initialized to 0.0 that will then be incremented as a result of calling the drive(double miles) method. It might also be mutated via a call to a method accelerate(double speed) and then stop(). At the same time, it's possible to design a Car object such that it has no mileage instance variable but instead keeps track of the average speed and total time the car has been in motion. The mileage might then be calculated based on those values.

So there's really no hard and fast rule on whether you would use a constructor or not or whether you use instance variables or not. It all depends on what ideas you want to represent and what kind of semantics makes most sense to do that.
 
Royale Summers
Ranch Hand
Posts: 87
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu -

What I understood from your statements is that both can and are used.

I haven't seen a real-world Java program yet but I thought about it some more and deduced that I should add to my understanding that it depends on what business requirement the code is needed to address determines the need of Instance Variables, Constructor, or both...

Thanks.
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!