Hi daniweb team,
I need to understand newinstance in java.So i tried the below program and got the error.
Can any one know how to write the newinstance program and explain the code.

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,
Prem

First of all what errors do you get?
Second, does the 'one' class has a 'newinstance' method? I don't see any so there is your error. You haven't defined a newinstance method.

Maybe you should try this:

try {
            one obj = (one)Class.forName("one").newInstance();
            obj.one();
        } catch (ClassCastException cce) {
            System.out.println("Error: "+cce.getMessage());
        }

And try to have class names begin with capital.
And don't have methods that have the same as the class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.