Hi Daniweb team,
I need to understand newinstance in java.I do no how to implement it and i tried this program but getting some errors.

1.Can anyone tell me how to use new Instance and what mistakes i have made in this program.?

import java.io.*;
public class sample
{
    public static void main(String args[])throws IOException{
        one obj=one.newinstance();
        obj.one();
    }
}
    class one{
        public static void one(){
        System.out.println("Sample program for new instances");
        }
    }

Thank you in advance,
Prem

Hi Prem,

I am not entirely sure what you mean, but I think you are saying that you are not really sure how constructors in java works (i.e. how to create a new instance of the object).

Since you have not explicitly defined a constructor, java will use the default constructor that is always present if not overwritten, which means that you can just say

public class sample
{
    public static void main(String args[])throws IOException{
        one obj = new one();
    }

    public static class one{}
}

However, a few items: 1) the class should be named One (standard practice is to capitalize all class names 2) One should either be located inside of the Sample class (and then most likely be static like you made it) or it should be located in its own java file and then NOT be static (it is implied by the fact that it's in its own file).
To get the functionality that you seem to want, this is how I would do it

public class sample
{
    public static void main(String args[])
        One obj = new One();
    }

    public static class one{
        public One()
        {
            System.out.println("Sample program for new instances");
        }
    }
}

I took away the IOexception since there is nothing there that will throw one presently, and the only other difference from the previous one is that the empty constructor has been overwritten, and will now print the statement. Otherwise they really are identical functionally.

The main point is that the constructor is really kind of a strange function that returns itself.

> Can anyone tell me how to use new Instance and what mistakes i
> have made in this program.? newInstance is actually a method of the Class and Constructor classes. You need to grab the class instance to invoke this method.

package com.you;

public class Test {
  public static void main(final String[] args) throws Exception {
    Test t = null;
    t = Test.class.newInstance(); // by hardcoding the class name
    // OR
    t = Class.forName("com.you.Test").newInstance(); // load class from name
  }
}

If you need to pass in arguments when creating a new instance, grab the relevant constructor and then call `newInstance` on it.

package com.you;

public class Test {

  public Test(String name) {
    System.out.println("Called string constructor");
  }

  public static void main(final String[] args) throws Exception {
    Constructor<Test> con = Test.class.getConstructor(String.class);
    Test t = con.newInstance("your-name");
  }
}

> Thank you in advance

HTH :-)

Edited 6 Years Ago by ~s.o.s~: n/a

This question has already been answered. Start a new discussion instead.