1 Java Building Blocks Flashcards

1
Q

There are only three rules to remember for legal identifi ers:
Which are those rules?

A

There are only three rules to remember for legal identifi ers:
■ The name must begin with a letter or the symbol $ or _.
■ Subsequent characters may also be numbers.
■ You cannot use the same name as a Java reserved word. As you might imagine, a reserved word is a keyword that Java has reserved so that you are not allowed to use it.
Remember that Java is case sensitive, so you can use versions of the keywords that only
differ in case. Please don’t, though.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When is required to the import to be static?

A

If in the code you refer to the static member or field only by the name, you will need a static import.

if the code refers to the class.STATIC_MEMBER_FIELD, then the import is not static, because you are importing the class and the static member will be referred to as class_name.static_member/field

package com.oca_revisions.imports;
puclic class TestClass {
    public static final int LOGIC_ID = 10;
    public static final int ANOTHER_STATIC = 200;
}

package com.oca_revisions;

import com.oca_revisions.imports; // imports the class
or
import com.oca_revisions.*; // all classes in that package

import static com.oca_revisions.imports.ANOTHER_STATIC;

puclic class TestClass2 {
    ...
     int test = Donkey.LOGICID + 10;
     int test_static = ANOTHER_STATIC; 
// import is static in this case because the static field is being referred to directly in the code, same applies to class members / methods
    ...
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What will the following code print when run?

public class TestClass{  
     public static long main(String[] args){      
         System.out.println("Hello");      
          return 10L;   
      } 
}
A
  • It will throw an Error at runtime.

When the program is run, the JVM looks for a method named main() which takes an array of Strings as input and returns nothing (i.e. the return type is void). But in this case, it doesn’t find such a method ( the given main() method is returning long!) so it gives out the following message:

Error: Main method must return a value of type void in class TestClass, please define the main method as: public static void main(String[] args)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly