Example usage for java.lang Class newInstance

List of usage examples for java.lang Class newInstance

Introduction

In this page you can find the example usage for java.lang Class newInstance.

Prototype

@CallerSensitive
@Deprecated(since = "9")
public T newInstance() throws InstantiationException, IllegalAccessException 

Source Link

Document

Creates a new instance of the class represented by this Class object.

Usage

From source file:example.Main.java

public static void main(String[] args) throws Exception {
    Class[] classes = new Class[3];
    classes[0] = A.class;
    classes[1] = B.class;
    classes[2] = C.class;

    JAXBContext jc = JAXBContext.newInstance(classes);

    JAXBIntrospector ji = jc.createJAXBIntrospector();
    Map<QName, Class> classByQName = new HashMap<QName, Class>(classes.length);
    for (Class clazz : classes) {
        QName qName = ji.getElementName(clazz.newInstance());
        if (null != qName) {
            classByQName.put(qName, clazz);
        }/*from w w w.  j a v  a  2 s .  c  om*/
    }

    QName qName = new QName("http://www.example.com", "EH");
    System.out.println(classByQName.get(qName));
}

From source file:MyClass.java

public static void main(String[] args) {
    Class<MyClass> ppClass = MyClass.class;
    try {//from   w ww  .j a v  a2 s .  co m
        MyClass p = ppClass.newInstance();
        Field name = ppClass.getField("name");
        String nameValue = (String) name.get(p);
        System.out.println("Current name is " + nameValue);
        name.set(p, "abc");
        nameValue = (String) name.get(p);
        System.out.println("New  name is " + nameValue);
    } catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException
            | IllegalArgumentException e) {
        System.out.println(e.getMessage());
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Date d = new Date();
    Class cls = d.getClass();
    System.out.println("Time = " + d.toString());

    Object obj = cls.newInstance();
    System.out.println("Time = " + obj);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    CachedRowSet rs;/*from w w w .jav a2  s .  com*/
    String ROWSET_IMPL_CLASS = "com.sun.rowset.CachedRowSetImpl";

    Class c = Class.forName(ROWSET_IMPL_CLASS);
    rs = (CachedRowSet) c.newInstance();

    rs.setUrl("jdbc:postgresql:dbname");
    rs.setUsername("username");
    rs.setPassword("password");

    rs.setCommand("select * from members where name like ?");
    rs.setString(1, "I%");

    rs.execute();

    while (rs.next()) {
        if (rs.getInt("id") == 42) {
            rs.setString(1, "newString");
            rs.updateRow(); // Normal JDBC

            rs.acceptChanges();
        }
    }
    rs.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class driverClass = Class.forName("org.hsqldb.jdbcDriver");
    DriverManager.registerDriver((Driver) driverClass.newInstance());

    // Print out all loaded JDBC drivers.
    java.util.Enumeration e = java.sql.DriverManager.getDrivers();
    while (e.hasMoreElements()) {
        Object driverAsObject = e.nextElement();
        System.out.println("JDBC Driver=" + driverAsObject);
    }/*from   w  w  w.j  ava  2 s  .  c  o  m*/

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File f = new File("c:/mysql-connector-java-5.1.18-bin.jar");
    URLClassLoader urlCl = new URLClassLoader(new URL[] { f.toURL() }, System.class.getClassLoader());

    Class mySqlDriver = urlCl.loadClass("com.mysql.jdbc.Driver");
    System.out.println(mySqlDriver.newInstance());
    System.out.println("Is this interface? = " + mySqlDriver.isInterface());

    Class interfaces[] = mySqlDriver.getInterfaces();
    int i = 1;//from   w  w  w  .ja v  a2 s .c  o  m
    for (Class _interface : interfaces) {
        System.out.println("Implemented Interface Name " + (i++) + " = " + _interface.getName());
    }

    Constructor constructors[] = mySqlDriver.getConstructors();
    for (Constructor constructor : constructors) {
        System.out.println("Constructor Name = " + constructor.getName());
        System.out.println("Is Constructor Accessible? = " + constructor.isAccessible());
    }
}

From source file:ConstructorTrouble.java

public static void main(String... args) {
    try {/* w  w  w  .  j av  a  2s . c  o  m*/
        Class<?> c = Class.forName("ConstructorTrouble");
        Object o = c.newInstance(); // InstantiationException

        // production code should handle these exceptions more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    } catch (InstantiationException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class c = Class.forName("MyClass");
    Method m = c.getDeclaredMethod("say", new Class[] { String.class, String.class });
    Object i = c.newInstance();
    Object r = m.invoke(i, new Object[] { "Hello", "World" });
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    CachedRowSet rs;/* w w  w. j  a v  a2  s.  c  o  m*/

    // Create the class with class.forName to avoid importing
    // from the unsupported com.sun packages.
    Class c = Class.forName(ROWSET_IMPL_CLASS);
    rs = (CachedRowSet) c.newInstance();

    rs.setUrl("jdbc:postgresql:tmclub");
    rs.setUsername("ian");
    rs.setPassword("secret");

    rs.setCommand("select * from members where name like ?");
    rs.setString(1, "I%");

    // This will cause the RowSet to connect, fetch its data, and
    // disconnect
    rs.execute();

    // Some time later, the client tries to do something.

    // Suppose we want to update data:
    while (rs.next()) {
        if (rs.getInt("id") == 42) {
            rs.setString(1, "Marvin");
            rs.updateRow(); // Normal JDBC

            // This additional call tells the CachedRowSet to connect
            // to its database and send the updated data back.
            rs.acceptChanges();
        }
    }

    // If we're all done...
    rs.close();
}

From source file:com.konakart.ApiExample.java

/**
 * @param args/*  w w  w  . j  a va2 s.c om*/
 */
public static void main(String[] args) {
    try {
        /*
         * Instantiate a KonaKart Engine instance
         */
        Class<?> engineClass = Class.forName(ws_engine_implementation);
        eng = (KKEngIf) engineClass.newInstance();

        /*
         * Login with default credentials
         */
        //sessionId = eng.login(DEFAULT_USERNAME, DEFAULT_PASSWORD);
        //log.info(DEFAULT_USERNAME + " logged in successfully and got session " + sessionId);

        log.info("Manufacturers:\n");
        ManufacturerIf[] manus = eng.getAllManufacturers();
        for (int m = 0; m < manus.length; m++) {
            log.info("\t" + manus[m].getName());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}