Is the `new` keyword in java redundant?

后端 未结 5 1465
闹比i
闹比i 2020-11-29 05:29

I am coming from C++ so there is one feature of java that I don\'t quite understand. I have read that all objects must be created using the keyword new, with th

5条回答
  •  借酒劲吻你
    2020-11-29 06:02

    The new must be written in Java to create a new object instance.

    public class Foo {
    
        public void test() {
            final Foo foo1 = new Foo();
            final Foo foo2 = Foo();
        }
    
        public Foo Foo() {
            System.out.println("hello world");
            return this;
        }
    }
    

    Note, that methods starting with an uppercase character are discouraged in Java to avoid the confusion.

提交回复
热议问题