aspose file tools*
The moose likes Beginning Java and the fly likes Object Creation in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object Creation in Java" Watch "Object Creation in Java" New topic
Author

Object Creation in Java

Rajesh Tarte
Ranch Hand

Joined: Nov 02, 2006
Posts: 33

When any constructor runs in java , does it create object?

SCJP 1.5 | SCWCD 5.0 |SCBCD
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9993
    
    7

Yes.
Beginner question. Moving.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Technically, the constructor itself is not the thing that creates the object. The JVM creates the object (i.e. allocates memory for it) and then calls the constructor to initialize the object.

But Maneesh is right, in that a constructor is only called when a new object is created and vice versa.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rajesh Tarte
Ranch Hand

Joined: Nov 02, 2006
Posts: 33

So If There is one class Parent
and other as Child

and Child extends Parent

ofcourse when we call new on child the Parent constructor also runs , so How many objects are created?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36566
    
  16
rajesh tarte wrote:So If There is one class Parent
and other as Child

and Child extends Parent

ofcourse when we call new on child the Parent constructor also runs , so How many objects are created?
1
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9993
    
    7

In case you are wondering about Campbell's answer, a call to a super constructor is not the same as calling new Parent()
Rajesh Tarte
Ranch Hand

Joined: Nov 02, 2006
Posts: 33

I also has the same answer but not the reason

As this is the refrence to current object say Child
then if there is no object for parent
then what Super represents?
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

say the child has 2 constructors and one of the constructors have this(), so how many objects should be created rakesh...think about it. well its on the same lines for the super too.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36566
    
  16
rajesh tarte wrote: . . .
As this is the reference to current object say Child
The keyword this on its own refers to the current object, as does this. with a dot. The statement this( . . . ); however is rather different. It means "call the other constructor on the current object and do part of the construction in that other constructor."

then if there is no object for parent
then what Super represents?
The only class with no superclass is java.lang.Object. So all other classes implicitly have java.lang.Object as a superclass.

I presume you can still work out how many objects are created?
Rajesh Tarte
Ranch Hand

Joined: Nov 02, 2006
Posts: 33

I know what is meant by this
My question was what Super represents if superclass object is not created?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36566
    
  16
It's not Super but super.

Objects are created in parts, rather like onions. There is a bit in the centre which is made from java.lang.Object. Around that there is a superclass bit. Then its subclass bit surrounds that . . .

["Centre" is metaphorical, not literal ]
Venu Chakravorty
Ranch Hand

Joined: Aug 19, 2009
Posts: 46
campbell you say that 1 object is created (i'm talking about the Child extends Parent example) although 3 constructors (Object, Parent and Child itself) are called? the Object and Parent constructors do not create any object(s)?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36566
    
  16
Welcome to JavaRanch

The creation process is described in the Java™ Language Specification. It is very simple and easy to understand (and if you believe that you'll believe anything ).

Grossly simplified version: the JVM creates an object and each constructor call initialises part of the object.
Venu Chakravorty
Ranch Hand

Joined: Aug 19, 2009
Posts: 46
thanks campbell, that link has a ton of info.
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
rajesh tarte wrote:I know what is meant by this
My question was what Super represents if superclass object is not created?


The keyword super (as the first statement in a constructor) is simply a call to another method -- it's a call to the constructor with the matching signature in the super class. (Similarly, the keyword "this" as the first statement in a constructor calls another constructor within the class.) Since you must call super, Java guarantees that programmer-specified initialization occurs for the entire hierarchy your class subclasses.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36566
    
  16
venu chakravorty wrote:thanks campbell, . . .
You're welcome
Rajesh Tarte
Ranch Hand

Joined: Nov 02, 2006
Posts: 33

Do you mean that it is not a reference?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36566
    
  16
rajesh tarte wrote:Do you mean that it is not a reference?
What's not a reference?
Rajesh Tarte
Ranch Hand

Joined: Nov 02, 2006
Posts: 33

As Per My understanding

this---> refrence to current class's object
and
super--> reference to parent class

Q1. are the above statements are true?
if no
how can you access any method or variable without using refrence?
if yes
then what is refrenced by super?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36566
    
  16
There isn't a current class. The this keyword without () means the current object and the super keyword without () means that part of the object created from the superclass and not overridden in the current object.

Somebody else might be able to explain better than me.
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
The people who designed Java were a bit stupid when it came to "this" and "super". When used as the first statement of a constructor, they have distinct meaning. "this" is normally a reference variable referencing the current object. But as the first statement in a constructor it's a method (constructor) call. Similarly, "super" as the first statement of a constructor is a call to the constructor in the superclass that matches the parameter signature -- "super("hi there")" is a call to the superclass constructor that takes a single string parameter. Everywhere else, "super" is a reference to non-overridden members of the superclass.

Again: Super and this have two distinct meanings. It's stupid that the Java designers used the same key word.
Rajesh Tarte
Ranch Hand

Joined: Nov 02, 2006
Posts: 33

I am not interesed in thease keywords are used as methods.
My view is second case where these are treated as references.

If there is refrence either it is null or referencing not null object

we can define same variable both in parent and child class

if we directly used the variable in child class it refers the child class version and when we use super.variable_name it looks for varible of parent although it is overriden

In fact if we do not create a same variable name we can use "this" to refer a varible from parent class provided that it is not private

This means along with the inherited properties and methods in child class some of the things are not accessible using "this" thats why we need super, is it so?
And there is no object other than child class is created
one more thing two object of type Class are created
This is what is my understanding
please correct me if I m wrong
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36566
    
  16
How can you get this pointing to null?
Any Class objects are loaded by the class loader program from the .class files on your drive. And at least three Class objects are loaded: the superclass, the subclass and java.lang.Object.
The super. reference is intended for use in an overriding method to gain access to the overridden version, and add functionality to that. If you are trying to give completely different functionality to a method, then you are not really overriding it (Google for "Liskov substitution principle" for more details).
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
rajesh tarte wrote:I am not interesed in thease keywords are used as methods.
If there is refrence either it is null or referencing not null object we can define same variable both in parent and child class if we directly used the variable in child class it refers the child class version and when we use super.variable_name it looks for varible of parent although it is overriden


Yes, the term for that is "shadowing", and you're right -- the way to reference the variable the superclass variable from the subclass is via "super.variableName". If you define an instance field in the ancestor, and another instance field of the same name in the subclass, then instances of the subclass have two variables allocated to it. One referenced via this.variableName in the ancestor and one referenced via this.variableName in the subclass.

Shadowing usually indicates a mistake, where the property is accidentally being re-implemented who doesn't notice the property has already been implemented in the ancestor. Shadowing can cause insidious bugs where two similar looking getters will reference two different variables depending on whether the getter is in the ancestor or subclass.
 
Don't get me started about those stupid light bulbs.
 
subject: Object Creation in Java
 
Similar Threads
Object meaning
Is java is purely object oriented or not?
InvocationTargetException when transferring a custom class in inheritance
aggregate object
Question for Chris Richardson : what do you call POJOs ?