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;
    }
}
全部评论

相关推荐

05-27 14:57
西北大学 golang
强大的社畜在走神:27届真不用急,可以搞点项目、竞赛再沉淀沉淀,我大二的时候还在天天打游戏呢
投递华为等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务