new_Keyword
new Java Keyword with Examples
The new keyword is used to create a new instance of a class.
The new Java Keyword Examples
The new keyword is used to create a new instance of a class.
Student student = new Student(“Tom”, 20);
The new keyword can be used to create a new array object:
// use the new keyword to create an int array object
int[] intArray = new int[10];
The new keyword can be used to create a new String array object:
// use the new keyword to create a String object
String string = new String();
Complete Example
package com.javaguides.corejava.keywords.newkeyword;
/**
* Demonstrates the usage of new keyword
* @author Ramesh Fadatare
*
*/
public class NewKeyword {
public static void main(String[] args) {
// use new keyword to create custom object type
Student student = new Student("Tom", 20);
// use new keyword to create int array object
int[] intArray = new int[10];
// use new keyword to create String object
String string = new String();
// use new keyword to create instance of Object
Object object = new Object();
}
}
class Student {
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
}