Wednesday, May 1, 2024
HomeJavaQuiz your self: Defining the construction of a Java class

Quiz your self: Defining the construction of a Java class


Oracle Java Certification, Java Tutorial and Materials, Java Guides, Oracle Java Certification, Oracle Java Learning


Check your data of Java lessons, equivalent to their legitimate names, the usage of variables inside a technique, and the variety of allowable import statements.

Which of the next statements are appropriate a few Java class? Select two.

A. A Java class will need to have a reputation proven within the supply code.

B. A Java class might have a number of native variables with the identical title inside the identical methodology.

C. A Java class might have a number of import statements.

D. An underscore character “_” is a legitimate Java class title.

Reply. Not all lessons have an specific title proven within the supply code. For instance, Java supplies nameless lessons, equivalent to the next:

Runnable r = new Runnable(){ public void run(){

  System.out.print(“Do nothing!”);

  }

};

r.run();

The run methodology of the Runnable interface is summary, and but you possibly can see that there’s a actual object since you instantiated it and might invoke the run methodology. This reveals that some concrete class, which implements the Runnable interface, exists. The variable r is a reference to an occasion of that class, however the class title isn’t identified within the supply code. Subsequently, choice A is wrong.

Variables are seen solely inside the scope wherein they have been outlined. Nonetheless, since a block bounded by curly braces defines a scope, you possibly can create two sibling scopes inside one methodology. If you happen to do that, two variables with the identical title can coexist and not using a drawback, equivalent to within the following:

void twoVars() {

  { int i = 0; }

  { int i = 1; } // OK

}

In view of this, choice B is appropriate.

Choice C discusses a number of import statements. Having a number of import statements isn’t merely permitted—typically, it’s essential to have many import statements to offer entry to lessons in numerous packages. It’s additionally typical to have a number of import statements offering entry to every of a number of lessons in the identical bundle, fairly than utilizing wildcards.

Even repeating import statements for a similar class or bundle is syntactically legitimate, although it could most likely set off a request throughout code evaluation to tidy up the code.

The next is totally authorized:

import java.util.*;

import java.util.*;

public class MyClass { // OK

}

By the way in which, if a category defines multiple bundle assertion—whether or not specifying the identical bundle title or a distinct bundle title—compilation would fail. Thus, the next wouldn’t compile:

bundle a.b.c;

bundle a.b.c; // NOT OK

public class MyClass {

}

As a result of choice C asks provided that a number of import statements are permitted, choice C is appropriate.

As for choice D, by Java 8 the one underscore character was a legitimate identifier and might be used as a category title, a technique title, or a variable title.

In Java 8, a warning throughout compilation indicated that this character was reserved for future language adjustments. Nonetheless, the warning didn’t stop its use. However then, starting with Java 9, the one underscore character was outlined as a key phrase and subsequently is not legitimate as an identifier.

This alteration was described in the Java 9 abstract of adjustments, which acknowledged that the underscore character was not a authorized title and warned that in case you use the underscore character (_) as an identifier, your supply code can’t be compiled.

By the way in which, it’s nonetheless authorized to make use of a double underscore (__) as an identifier, equivalent to for a category title, a technique title, or a variable title. You can even begin a variable title with a single underscore.

public class MyClass {

    int __; // Double underscore is OK

}

As a result of the one underscore is a key phrase and isn’t a authorized identifier by itself any longer, choice D is wrong.

Conclusion. The right solutions are choices B and C.

Supply: oracle.com

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments