• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why do we create a constructor?  RSS feed

 
Raja Pathak
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AS A NEW TO JAVA PROGRAMMING I WANT TO KNOW WHY WE CREATE CONSTRUCTOR
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find this in any textbook or tutorial.

Also, please don't SHOUT.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch. Please KeepItDown - don't use ALL CAPITALS BECAUSE IT LOOKS LIKE YOU ARE SHOUTING!

A constructor is a special block of code that you can include in a class which is executed when you create a new instance of the class. It's meant to initialize new objects of that class.

See Providing Constructors for Your Classes in Oracle's Java Tutorials for more information.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you need to get the object into a “consistent” state. That needs code to initialise it, and set up the initial values of all its fields. A constructor both allows you to take information from outside, and to specify what sort of information to take. Look at these old posts, which might help you : 1 2 3 4 Note I argued about the necessity for constructors, and the necessity for few constructors.
 
sandeep kulkarnii
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constructor is a special method in a class which is executed when an instance of a class is created. It is used to initialize the non - static members of a class.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sandeep kulkarnii wrote:A constructor is a special method in a class which is executed when an instance of a class is created. It is used to initialize the non - static members of a class.


When you quote somebody else's writing, please cite your source.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote: . . . When you quote somebody else's writing, please cite your source.
In this case, so we can avoid it, because there are at least two errors in that quote.
 
Raja Pathak
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please dont explain using oops.
just tell what is its use in programming(for the one who dont know what a object is).
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raja Pathak wrote:please dont explain using oops.
just tell what is its use in programming(for the one who dont know what a object is).


If you don't know what an object is, there's no point in explaining a constructor. A constructor is meaningless without an object, and you won't be able to understand any explanation if you don't know what an object is.

If you're going to be programming in Java, you'll have to learn what an object is anyway, so learn that first, and then come back to the constructor question.
 
Raja Pathak
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay
can you give an example of its simplicity when we used constructor in the code.How would the code change when we do not use constructor
 
abrar alvi
Ranch Hand
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constructor has the code that runs before the creation of an object in java i.e the code that runs when you say new on a class type...



also keep in mind every class gets a constructor even if you don't write it.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raja Pathak wrote:okay
can you give an example of its simplicity when we used constructor in the code.


I don't understand what you're asking, but if you want examples of constructors, you can find them easily with a web search of by looking at any tutorial.

How would the code change when we do not use constructor


The same way as it would be different by removing any other code. Whatever that code does wouldn't get done. And, as already stated a couple of times, what a constructor's code does--or at least what it's supposed to do--is put a newly created object into a valid initial state. Mostly that means setting member variables appropriately.

And, again: All this information is available in any intro to Java book or tutorial. Your best bet is to study one or more of those and then come back with more specific questions about what you don't understand.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abrar alvi wrote:A constructor has the code that runs before the creation of an object in java


That's not true. The object has already been created (by the new operator, usually) before the constructor is invoked.

 
abrar alvi
Ranch Hand
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
abrar alvi wrote:A constructor has the code that runs before the creation of an object in java


That's not true. The object has already been created (by the new operator, usually) before the constructor is invoked.



as per my knowledge which i got from head first java... its given that

a constructor has the code that runs when you instantiate an object....


can you explain this Jeff


thanks
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abrar alvi wrote:
Jeff Verdegan wrote:
abrar alvi wrote:A constructor has the code that runs before the creation of an object in java


That's not true. The object has already been created (by the new operator, usually) before the constructor is invoked.



as per my knowledge which i got from head first java... its given that

a constructor has the code that runs when you instantiate an object....


This is true. It's part of the instantiation process. First the object is created (memory is reserved, member variables are given their default values, etc.) and then the chosen constructor is executed. Details can be found at JLS 15.9.4 Run-time Evaluation of Class Instance Creation Expressions.

A constructor is part of the instance creation process (instantiation) but it is not run before the object is created.

 
sandeep kulkarnii
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
sandeep kulkarnii wrote:A constructor is a special method in a class which is executed when an instance of a class is created. It is used to initialize the non - static members of a class.


When you quote somebody else's writing, please cite your source.


dude, I don't know whose information I have copyrighted.But that was the information provided by our tutor.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sandeep kulkarnii wrote:dude, I don't know whose information I have copyrighted.But that was the information provided by our tutor.

Fine, then all you need to say is: "According to my tutor ...".

Winston

 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:don't use ALL CAPITALS BECAUSE IT LOOKS LIKE YOU ARE


... a capitalist.

;-)
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!