• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Why do we create a constructor?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AS A NEW TO JAVA PROGRAMMING I WANT TO KNOW WHY WE CREATE CONSTRUCTOR
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can find this in any textbook or tutorial.

Also, please don't SHOUT.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Rancher
Posts: 1043
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:don't use ALL CAPITALS BECAUSE IT LOOKS LIKE YOU ARE



... a capitalist.

;-)
 
Did you ever grow anything in the garden of your mind? - Fred Rogers. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic