0

This question is because of this line i read A new instance of an object is created by calling a constructor method. i agrre but constructor method of what..? an OObject or a Class itself..?.. Sorry if its an amateur question but i'm still learning java and i'm finding it hard to understand.

and this reminded me of which is first chicken or egg..?

EDIT:

May be my question was not clear, i know how objects are created and every class has a constructor but what i want to know is, every Objects in the heap have their own copy of instance variable. in the same way will they also have a Constructor with them or its just something that only classes have.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
ngesh
  • 13,398
  • 4
  • 44
  • 60

7 Answers7

5

In Object-oriented design, a constructor is what creates an object out of your class definition...

2 key concepts here

  • Class - a class is the blueprint for what an instantiated object should contain, both behaviour (methods) and information (properties). Usually contains a Constructor.

  • Object - The thing that is Created by the constructor, a instatiated version of the Class in practical use.

Example of a Constructor in use

public class MyClass {

    public int intProperty;

    // This is the Constructor, Notice it shares a name with the Class
    public MyClass(int value) {
        intProperty = value;
    }
}

Now to Use the class

//                            |----------This is calling the constructor
//                            |          and placing a new MyClass object
//                            v          in myClassObejct
MyClass myClassObject = new MyClass(3);

myClassObject.intProperty; // 3

This creates a new MyClass Object

Java does not work without Classes and Constructors, it is core to the design pattern of the language...

Only Classes have constructors, Objects are the product of Constructors, an Object itself does not contain a constructor.

jondavidjohn
  • 61,812
  • 21
  • 118
  • 158
  • see my addition... Java does not work without Classes and Constructors, it is core to the design pattern of the language... – jondavidjohn Sep 23 '11 at 05:41
  • @ntc: do you mean a Java object, or THE Java java.lang.Object Class? http://download.oracle.com/javase/1,5.0/docs/api/java/lang/Object.html – Daryl Teo Sep 23 '11 at 05:45
  • its small "O", just object and not Object – ngesh Sep 23 '11 at 05:49
  • @ntc okay then yes, there is no such thing as a object constructor. An object (I prefer to call them instance) is created when the constructor for the >> class << is called. – Daryl Teo Sep 23 '11 at 05:56
  • @Daryl Teo.. Thanks dude.. i got it. – ngesh Sep 23 '11 at 05:59
2

Yes, you call the constructor of the class:

MyClass instance = new MyClass();

but note that some objects have special constructors allowed by the language, for example String:

String x = "foobar";

Is very similar to (but not exactly the same as):

String x = new String("foobar");

Note that if no constructors are defined for the class, a default (no-args) constructor is implied .

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Actually I don't believe this is actually true. if i'm not wrong (and I often am) "foobar" is actually a static string instance. For example, if you have String x = "foobar"; String y = "foobar"; and do x == y, it will give you True (because they are actually the same instance, and not creating a new instance). This is also the reason why many first time java programmers make the mistake of using equality operator instead of "compareTo" or "equals" when comparing strings; it works when the string exists at compiletime, but fails when the string is created at runtime (through user input etc.) – Daryl Teo Sep 23 '11 at 05:31
  • That's why I said "not exactly the same as". For most normal situations, it can be considered "the same" (ie you shouldn't be using `==` to compare Strings) – Bohemian Sep 23 '11 at 05:32
  • so you mean to say Constructor of a class is called when creating an Object and not the Constructor of An Object itself..? – ngesh Sep 23 '11 at 05:33
  • @Bohemian By "some objects" you mean strings and wrappers to primitives, right? – NullUserException Sep 23 '11 at 05:33
  • @NullUserException - yes. I was trying to simplify the answer to not scare newbie OP – Bohemian Sep 23 '11 at 05:34
  • @Bohemian maybe I'm being nitpicky, and I understand where you are coming from, but by calling it a special constructor, it implies you're creating a new instance of it when you put "foobar". Just wanted to clear that up in case it causes confusion :) – Daryl Teo Sep 23 '11 at 05:36
  • @NullUserExceptionఠ_ఠ I haven't used Java in a couple of years... can you actually go Integer i = 10? I thought you had to put Integer i = new Integer(10); – Daryl Teo Sep 23 '11 at 05:47
  • @NullUserExceptionఠ_ఠ cool tool! Didn't know about this. Seems like it does the same thing with Integers as well. http://ideone.com/DweYS ah well I'm just being pedantic :D doesn't really add to the question anyway. – Daryl Teo Sep 23 '11 at 05:53
  • @DarylTeo Integers up to a certain size are cached (127 IIRC). If you create new Integers bigger than that, the comparison will be false. http://ideone.com/qqFjF – NullUserException Sep 23 '11 at 06:05
  • @NullUserExceptionఠ_ఠ ooh that is interesting. Do you have any idea what the justification for that is? Long strings do not seem to have the same issue. http://ideone.com/JYOdM maybe I will post question on Programmers :D – Daryl Teo Sep 23 '11 at 06:10
  • 1
    @DarylTeo http://stackoverflow.com/questions/5277881/why-arent-integers-cached-in-java – NullUserException Sep 23 '11 at 06:14
2

Yes every class has a constructor.

If you do not explicitly define one, Java will create a default empty one for you.

Daryl Teo
  • 5,394
  • 1
  • 31
  • 37
1

In Java every class has one or more constructors, and when you create an object using the new keyword, a constructor of that class is called.

example:

Integer i = new Integer("1");
//i is the object, and the Integer class constructor gets called
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
1

Every class must have its own constructor. Either you provide one or more ctor's or the compiler generates a default no-argument ctor for you. If you have your own ctor, than the compiler doesn't generate anything.

ChrLipp
  • 15,526
  • 10
  • 75
  • 107
1

Every object in Java must be created through some Class's constructor, with the exception of a few primitive classes like String, which has special allocation rules. Even at the most basic level, you can always call

Object o = new Object();

and since all objects in Java inherit from the Object superclass, most objects will inherit the default constructor.

The exception to this is when a class only has a private constructor--one that cannot be called by any outside classes. In that case, because the default constructor is no longer necessary, it will not be accessible either.

class MyClass
{
    private MyClass()
    {
        //cannot be called by outside classes
    }
}

In general, this is used either for purely static classes (which don't need instances) or Singleton objects (that want to restrict instantiation). Therefore all objects are created from some constructor, but not all classes necessarily have a usable constructor.

donnyton
  • 5,874
  • 9
  • 42
  • 60
0

The statement "A new instance of an object is created by calling a constructor method" is an incorrect statement and is what's causing the confusion. There's no such thing as an instance of an object. An object is an instance of a class. The following 2 statements are valid:

An object is created by calling a constructor method.

An instance of a class (i.e. an object) is created by calling a constructor method.

gary
  • 1