Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

what actually constructor returns

 
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Constructor does not allocate memory for the object/instance, that is done by new. what is the role of constructor is to initialize the instance variables. Now there are two issues which needs attention and I'd like to confirm with you ranchers

1. when I donot write any constructor in my class Java provides one, which we usually say that intializes the instance variables to their default values, but I think default construtor doesn't do anything as any variable screated on heap is automatically initialized, am I right?

2. What does constructor returns ? Current Object i.e this that means object reference as this is a reference to the current object. So I understand the process as such. new allocates memry and constructor is called by passing this to the constructor and the only code which is present in default constructor is return this and in paramterized constructor , return this statement is automatically added?

Any suggestions or additions....

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

Nancy Antony wrote: when I donot write any constructor in my class Java provides one, which we usually say that intializes the instance variables to their default values


This no-arg constructor provided by Java does not initialize the instance variables to default values. The initialization happens even before this constructor is called.

but I think default construtor doesn't do anything as any variable screated on heap is automatically initialized, am I right?


Yes, that's correct.

What does constructor returns ? Current Object i.e this that means object reference as this is a reference to the current object. So I understand the process as such. new allocates memry and constructor is called by passing this to the constructor and the only code which is present in default constructor is return this and in paramterized constructor , return this statement is automatically added?


It's the new operator that calls the constructor and finally returns the reference to the new object. A constructor has no return type hence doesn't ever return anything.
 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nancy Antony wrote:I think default construtor doesn't do anything as any variable screated on heap is automatically initialized, am I right?


Well not completely automatically. Objects in the heap too need to be assigned a value. While its right that the default constructor doesn't provide a value to the instance variables, they are provided a default value by the compiler at their declarations. So if I write this code
Then after compilation this code would look something like this
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Nancy,

Constructor can't return anything as it doesn't have any return type , what it ensures that super class gets initialized before sub class gets initilized by calling super() on first line.

Thanks
Javin
 
Greenhorn
Posts: 5
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why the constructor doesn't return any values
 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:
Then after compilation this code would look something like this


Objects are formed at run time not at compile time. Then how can instance variables be initialized at compile time before instance of class is formed? Are they initialized at the time when class is loaded?
 
Bharaathi Jagadisan
Greenhorn
Posts: 5
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Briefly explain about the constructor and why we are using it
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bharaathi Jagadisan wrote:Briefly explain about the constructor and why we are using it



This will explain the basics for you
 
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

Bharaathi Jagadisan wrote:why the constructor doesn't return any values


A constructor is a special block of code that's called to initialize a new instance of a class. It doesn't need to return a value. Even if it were possible to return a value, then the Java language wouldn't have any syntax to get at this value.

The question is like asking why a cat can't ride a bicycle. It's because it's a cat, and cats just don't ride bicycles. Likewise, a constructor just doesn't return a value. That's just how it's defined in the Java programming language.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:cats just don't ride bicycles


Not even on youtube ? They do just about everything else on there.
 
Bharaathi Jagadisan
Greenhorn
Posts: 5
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Astha Sharma wrote:
Objects are formed at run time not at compile time. Then how can instance variables be initialized at compile time before instance of class is formed? Are they initialized at the time when class is loaded?


What Ankit meant to say was that if you write the code asthen, the compiler (while compiling) "sees" or perceives it like
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok...thanks
 
You showed up just in time for the waffles! And this 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
reply
    Bookmark Topic Watch Topic
  • New Topic