5 Replies - 306 Views - Last Post: 28 December 2012 - 11:21 AM Rate Topic: -----

#1 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 08-September 12

Constructors and the creation of objects.

Posted 28 December 2012 - 09:46 AM

Creation of objects is done via the format
ClassName variableName = new ClassName();


But a constructor creates an instance of an object. This gets confusing. The construtor format is
public ClassName
. But it doesn't create any new object at all ?

My mind is boggled at the moment. Any clarification is greatly appreciated. :donatello:

Is This A Good Question/Topic? 0
  • +

Replies To: Constructors and the creation of objects.

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: Constructors and the creation of objects.

Posted 28 December 2012 - 09:50 AM

it creates an instance of the class.
Was This Post Helpful? 0
  • +
  • -

#3 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 08-September 12

Re: Constructors and the creation of objects.

Posted 28 December 2012 - 09:57 AM

View Postdarek9576, on 28 December 2012 - 09:50 AM, said:

it creates an instance of the class.

You are referring to the Object creation right ?

Hmm I read that instance = object.

So following that theory, an instance of an object = an object of an object ?

Doesn't sound right lol :whatsthat:
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5906
  • View blog
  • Posts: 12,811
  • Joined: 16-October 07

Re: Constructors and the creation of objects.

Posted 28 December 2012 - 11:03 AM

Understand, "instance of a class" and "object" are basically synonymous.

View PostGisengryl, on 28 December 2012 - 11:46 AM, said:

Creation of objects is done via the format
ClassName variableName = new ClassName();


This is correct. Sort of. You're doing several things here. You are creating an instance of a class using no parameters in the constructor and returning the resulting object to variableName.

The constructor of a class creates nothing. Rather, the constructor is called when the object is created. It is used to initialize the object with its starting values.
Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7961
  • View blog
  • Posts: 13,580
  • Joined: 19-March 11

Re: Constructors and the creation of objects.

Posted 28 December 2012 - 11:12 AM

View Postbaavgai, on 28 December 2012 - 01:03 PM, said:

The constructor of a class creates nothing. Rather, the constructor is called when the object is created. It is used to initialize the object with its starting values.



Correct. From the JLS, the process is as follows:

Quote

15.9.4. Run-time Evaluation of Class Instance Creation Expressions

At run-time, evaluation of a class instance creation expression is as follows.

First, if the class instance creation expression is a qualified class instance creation expression, the qualifying primary expression is evaluated. If the qualifying expression evaluates to null, a NullPointerException is raised, and the class instance creation expression completes abruptly. If the qualifying expression completes abruptly, the class instance creation expression completes abruptly for the same reason.

Next, space is allocated for the new class instance. If there is insufficient space to allocate the object, evaluation of the class instance creation expression completes abruptly by throwing an OutOfMemoryError.

The new object contains new instances of all the fields declared in the specified class type and all its superclasses. As each new field instance is created, it is initialized to its default value (4.12.5).

Next, the actual arguments to the constructor are evaluated, left-to-right. If any of the argument evaluations completes abruptly, any argument expressions to its right are not evaluated, and the class instance creation expression completes abruptly for the same reason.

Next, the selected constructor of the specified class type is invoked. This results in invoking at least one constructor for each superclass of the class type. This process can be directed by explicit constructor invocation statements (8.8) and is described in detail in 12.5.

The value of a class instance creation expression is a reference to the newly created object of the specified class. Every time the expression is evaluated, a fresh object is created.




Gisengry said:

So following that theory, an instance of an object = an object of an object ?


A class is a template: it is the model on which an object is built. An instance is the thing itself. "Object" is used a little more flexibly than we'd like, so generally if it matters we'll say "class" and "instance". An instance is the thing that has its own location in memory and its own data.
Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Constructors and the creation of objects.

Posted 28 December 2012 - 11:21 AM

View PostGisengryl, on 28 December 2012 - 12:46 PM, said:

The construtor format is
public ClassName
.


Not true. This is more like a class declaration (with the class keyword missing)
The constructor of an object is

public ClassName() {   // <--- with the () 


This post has been edited by pbl: 28 December 2012 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1