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;
}
}
All Java Keywords
- abstract Java Keyword
- assert Java Keyword
- boolean Java Keyword
- break Java Keyword
- byte Java Keyword
- case Java Keyword
- catch Java Keyword
- char Java Keyword
- class Java Keyword
- continue Java Keyword
- default Java Keyword
- do Java Keyword
- double Java Keyword
- else Java Keyword
- enum Java Keyword
- extends Java Keyword
- final Java Keyword
- finally Java Keyword
- float Java Keyword
- for Java Keyword
- if Java Keyword
- implements Java Keyword
- import Java Keyword
- instanceof Java Keyword
- int Java Keyword
- interface Java Keyword
- long Java Keyword
- native Java Keyword
- new Java Keyword
- package Java Keyword
- private Java Keyword
- protected Java Keyword
- public Java Keyword
- return Java Keyword
- short Java Keyword
- static Java Keyword
- strictfp Java Keyword
- super Java Keyword
- switch Java Keyword
- synchronized Java Keyword
- this Java Keyword
- throw Java Keyword
- throws Java Keyword
- transient Java Keyword
- try Java Keyword
- void Java Keyword
- volatile Java Keyword
- while Java Keyword
Comments
Post a Comment
Leave Comment