Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is java new? A function or keyword

Tags:

As i understand new is a keyword and not a function.

For example

A a = new A(); 

instantiates the object a of type A.
A keyword is not associated with any object per se.

On the contrary, when we have in A a public inner class B we call

B b = a.new B() 

Here it looks like new is a property of B and not an independent keyword.

What is the meaning of A.new ?

like image 757
David Michael Gang Avatar asked May 13 '13 14:05

David Michael Gang


People also ask

Is new a keyword or operator in Java?

Instantiation: The new keyword is a Java operator that creates the object. Initialization: The new operator is followed by a call to a constructor, which initializes the new object.

What is new function in Java?

The new operator is used in Java to create new objects. It can also be used to create an array object. Let us first see the steps when creating an object from a class − Declaration − A variable declaration with a variable name with an object type. Instantiation − The 'new' keyword is used to create the object.

Is function a Java keyword?

A Java keyword is one of 50 reserved terms that have a special function and a set definition in the Java programming language. The fact that the terms are reserved means that they cannot be used as identifiers for any other program elements, including classes, subclasses, variables, methods and objects.

What is keyword in Java?

In the Java programming language, a keyword is any one of 67 reserved words that have a predefined meaning in the language. Because of this, programmers cannot use keywords in some contexts, such as names for variables, methods, classes, or as any other identifier.


1 Answers

New is a keyword in both cases. It's part of a class instance creation expression.

There are two forms: unqualified and qualified.

The unqualified form starts with the keyword 'new'.

The qualified form starts with a primary class, then 'new'. This allows creation of inner classes -- non-static nested classes that hold an implicit reference to an instance of the outer class. The qualified form provides a way to specify that instance.

From the Java Language Specification, section 15.9:

Unqualified class instance creation expressions begin with the keyword new.

An unqualified class instance creation expression may be used to create an instance of a class, regardless of whether the class is a top level (§7.6), member (§8.5, §9.5), local (§14.3) or anonymous class (§15.9.5).

Qualified class instance creation expressions begin with a Primary.

A qualified class instance creation expression enables the creation of instances of inner member classes and their anonymous subclasses.

like image 114
Andy Thomas Avatar answered Oct 05 '22 05:10

Andy Thomas