• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

something about constructor

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello
sir i just want to ask that waht all the constructor do
and what will have when we have default consstructor in a class
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Constructor's main purpose is to intialize your object. When you are dealing with non-visual objects, this can be kinda hard to understand, but basically it set's your variables values and allocates memory space for them (not necassarily it that order).
An easier way to see this, is if you create a Java application that has a frame, when you code the object, you would put all of the code dealing with how big your frame is, the layout, the elements in it, and all of that in the contructor. If you are familiar with applets at all, basically everything you would include in init() methond in an applet, you would put in a constructor for an application.
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say you have a couple JDialogs that are similiar. Would it be best to have overloaded constructors and pass objects of the different dialogs you want or to have separate JDialog classes for each different one. I've created one class for four different Dialogs and my code is getting ugly. I've actually constructed a bunch of "if" statements that control what to do for what is passed to the BasicJDialog class. Instead of overloaded constructors I have private instances of Strings. So if I called new BasicJDialog( "NEW_USER" ). It uses "if" statements to figure out the layout of the NEW_USER object. I'm thinking I've taken the wrong approach. I'm thinking it would probably be better to take the overloaded constructor way. My code would look better and if I ever had another Dialog to create I would just write another overloaded constructer of that object to handle it. Does this sound like the right approach?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused about constructors too!
I just started class, so I hope this doesn't seem too trivial a question, but why use a constructor instead of assigning values right when you declare the variable?
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For starters, you can't use direct assignment to set up an object with values supplied by another source. As an example, what about java.awt.Button? You can't very well use direct assignment in the class to give a Button a String label, unless all Buttons should have the same label.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
[This message has been edited by Michael Ernest (edited February 11, 2001).]
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several reasons I can think of straight off for having constructors rather than assigning in the 'calling' program:
a) Consistency. With constructors, you KNOW the object will be initialised correctly.
b) Encapsulation. What if you decided to modify what variables are in the class? You wouldn't want to modify every bit of code that created the object.
c) Organisation. The code that manipulates the class (and that includes setting values) is all in the one place - the classes .java file.
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic