new Java Keyword with Examples

The new keyword is used to create a new instance of a class.

The new Java Keyword Examples

  1. The new keyword is used to create a new instance of a class.
Student student = new Student("Tom", 20);
  1. 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];
  1. 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 

  1. abstract Java Keyword
  2. assert Java Keyword
  3. boolean Java Keyword
  4. break Java Keyword
  5. byte Java Keyword
  6. case Java Keyword
  7. catch Java Keyword
  8. char Java Keyword
  9. class Java Keyword
  10. continue Java Keyword
  11. default Java Keyword
  12. do Java Keyword
  13. double Java Keyword
  14. else Java Keyword
  15. enum Java Keyword
  16. extends Java Keyword
  17. final Java Keyword
  18. finally Java Keyword
  19. float Java Keyword
  20. for Java Keyword
  21. if Java Keyword
  22. implements Java Keyword
  23. import Java Keyword
  24. instanceof Java Keyword
  25. int Java Keyword
  26. interface Java Keyword
  27. long Java Keyword
  28. native Java Keyword
  29. new Java Keyword
  30. package Java Keyword
  31. private Java Keyword
  32. protected Java Keyword
  33. public Java Keyword
  34. return Java Keyword
  35. short Java Keyword
  36. static Java Keyword
  37. strictfp Java Keyword
  38. super Java Keyword
  39. switch Java Keyword
  40. synchronized Java Keyword
  41. this Java Keyword
  42. throw Java Keyword
  43. throws Java Keyword
  44. transient Java Keyword
  45. try Java Keyword
  46. void Java Keyword
  47. volatile Java Keyword
  48. while Java Keyword

Comments