DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   what is the effect of using constructors in the program? (http://www.daniweb.com/forums/thread13749.html)

psylocke Nov 9th, 2004 2:06 am
what is the effect of using constructors in the program?
 
i dont know exactly why constructors are being used, it is used in what purpose?

Narue Nov 9th, 2004 9:00 am
Re: what is the effect of using constructors in the program?
 
>i dont know exactly why constructors are being used
Not using a constructor is akin to a restaurant giving you a glass without the drink. In the simplest manner, constructors guarantee that your object is created with predictable data for the methods to work with.

Phaelax Nov 9th, 2004 8:02 pm
Re: what is the effect of using constructors in the program?
 
sounds like he hasn't covered objects yet in class. Here's an example where a constructor takes in 1 string.

MyClass person = new MyClass("name");

The object "person" now has an attribute already set to that string. Without a constructor, you would have to do this:

MyClass person = new MyClass();
person.setName("name");

Narue Nov 9th, 2004 8:17 pm
Re: what is the effect of using constructors in the program?
 
Quote:

Originally Posted by Phaelax
sounds like he hasn't covered objects yet in class. Here's an example where a constructor takes in 1 string.

MyClass person = new MyClass("name");

The object "person" now has an attribute already set to that string. Without a constructor, you would have to do this:

MyClass person = new MyClass();
person.setName("name");

>sounds like he hasn't covered objects yet
Right. So giving an example that uses objects, constructors, new, methods, and arguments, and fails to explain what's meant by object, string, and attribute is soooo informative. :rolleyes:

Phaelax Nov 10th, 2004 11:49 am
Re: what is the effect of using constructors in the program?
 
oops:cheesy:

when i learned about objects, i was told what they were but never understood it until i saw code, but thats just how i learn. seeing code and figuring it out rather than listening to what it is. I wrote a document awhile ago about what objects are and how to get started with java programming using BlueJ. The article's website isn't online anymore, but I can try to locate it later for you.

nanosani Nov 10th, 2004 3:05 pm
Re: what is the effect of using constructors in the program?
 
Constructors simply initialize your program ... intialize means that what do you want your program to do automatically when it starts ... for example if you make a GUI (Graphical User Interface) you'll want your program to display the window and all the buttons and fields etc when the program starts ... so you'll write all these in your constructor.


All times are GMT -4. The time now is 6:46 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC