March 5, 2013

Completing a class definition

Question by user2086204

Suppose that you are given the following PetDriver class, which includes a main method:

public class PetDriver{
  public static void main(String[] args){
    int weight = 40;
    Pet doggie = new Pet("Rover", weight);
    System.out.println("my pet's name is " + doggie.getName());
    System.out.println("my pet's weight is " + doggie.getWeight());
  }
}

Executing main produces the following output:

my pet’s name is Rover
my pet’s weight is 40

My code is as follows but It is returning null.

public class pet {

    public String name;
    public int weight = 40;

    public Pet (String y, int x){
        y = name;
        x = weight;
    }

    public String getName(){
        return name;
    }

    public int getWeight(){
        return weight;
    }
}

Thanks!

Answer by Starx

Your constructor does not assign the value to the properties. Change them to the following.

public Pet (String y, int x){
  name = y;
  weight = x;
}
January 17, 2012

What is the return type of a constructor in java?

Question by sonu thomas

As we know the we do not have to add any return type to a Java constructor.

class Sample{
  .....
  Sample(){
    ........
  }
}

In Objective C, if we create a constructor, it returns a pointer to its class. But it is not compulsory, I think.

AClass *anObject = [[AClass alloc] init];//init is the constructor with return type a pointer to AClass

Similarly, Is the constructor converted to a method which return a reference to its own class??

Like this:

class Sample{
    .....
    Sample Sample(){
      ........

      return this;
    }
}

Does the compiler add a return type a reference to same class to constructor?
What is happening to a constructor?
Any reference to study this?

EDIT:

Actually i want the answers to be at byte code level or JVM level or even below.

Answer by Peter Lawrey

Many have answered how constructors are define in Java.

At the JVM level, static initialisers and constructors are methods which return void. static initialisers are static methods, however constructors use this and don’t need to return anything. This is because the caller is responsible for creating the object (not the constructor)

If you try to only create an object in byte code without calling a constructor you get a VerifyError. However on the oracle JVM you can use Unsafe.allocateInstance() to create an object without calling a constructor,

The static initialiser is called <cinit> which takes no arguments and and the constructor is called <init>. Both have a void return type.

For the most part this is hidden from the Java developer (unless they are generating byte code) however the only time you see these “methods” in stack traces (though you can’t see a return type)

Answer by Starx

Is the constructor converted to a method which return a reference to its own class??

No but yes, if it is specified to do so.

Does compiler add a return type a reference to same class to constructor ??

No it does not

What is happening to a constructor??

It is the method, which runs when the object is created. Typically, by using “new” keyword. It Might perform some preliminary task, or return something or assign some values during construction.

Any reference to study this.??

...

Please fill the form - I will response as fast as I can!