• Post Reply Bookmark Topic Watch Topic
  • New Topic

Whats the purpose of a Constructor ?  RSS feed

 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the purpose of constructors. I find them kinda useless, because I can use method.
Other websites says that they create "instance". I find this. beig quite useless, because I can initialize value for integers instantly, without rewriting it few times.



 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, constructor at 10th line for some reason dont works, as compilier marks as mistake. Why?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:Also, constructor at 10th line for some reason dont works, as compilier marks as mistake. Why?


Declaring a constructor inside a method is not allowed.

Henry
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors create an object in essence, you are constructing an object into memory. A constructor allows you to create a new variable or instance of an object.

I'm on a mobile device so I can't really respond in detail like I would like to. Once I get home, I will.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote: beig quite useless, because I can initialize value for integers instantly, without rewriting it few times.

Java has a feature called autoboxing that automatically creates the object version of an int primitive (and others) as needed. You can't create a Klas or Begin object without typing the "new" keyword.

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say you have a Circle class:

Now say you write this:

Do you see how the constructor allows you to create circles of different radiuses?
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to what others have said, here is a small program to demonstrate a few stuff:

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:What is the purpose of constructors. I find them kinda useless, because I can use method.
Practice is good, very good, but do you read some books along with practice? Some theory is also needed. Get "Head First Java", it is quite good book for beginners.

One more thing - get rid of the habbit to write programs in your native language. Programs should be written in the most popular and most commonly used language - which is English, that way more developers around the world could read your code - that is important. Maybe not now, but will be in the near future when you work for some company. You may write programs, but somebody will need to maintain them later, and that person not necessarily will be you. Could be anybody from all around the world.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Let's say you have a Circle class:

Now say you write this:

Do you see how the constructor allows you to create circles of different radiuses?


The same can be done with method?
Thanks for code.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same can be done with method? 

Yes it can.  You can use the Bean Pattern:

However, here's the disadvantage of that pattern:

In between the line that creates the circle object and the setting of the radius, you have a circle without a radius.  What does that mean?  It's not a valid state.  Also, you have to remember (and all users of the Circle class have to know) to set the radius.  In a small class like this it doesn't make much difference, but in a larger class, especially if you don't have the source code, it can be a problem.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:In a small class like this it doesn't make much difference, but in a larger class, especially if you don't have the source code, it can be a problem.


I think that this may be the root of the issue. The examples used are so simple that there really isn't much that constructors can solve, than can't be done with "initialization methods". And of course, since the OP is a learner, all the examples are designed to be simple.

Anyway, to the OP ... I think that there needs to be some trust (that there's a purpose) during the learning process, because with simple examples, you can also argue that ...

  • there is no reason for the "for" (both variants) or "do" loops. You can do everything with "while" loops, which are simpler to understand.
  • there is no reason for methods, as it just jumbles code everywhere, in all different places.
  • there is no reason for objects, as primitive types (with static code) work fine. And are simpler to understand.
  • there is no reason for generics.
  • there is no reason for inner classes.

  • etc...

    Henry
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!