Java Questions 21 - 30  «Prev  Next»

Java Serialization and Enum Questions

  1. What happens when you declare a variable as transient?

    Answer:
    A variable marked transient tells the JVM to ignore the variable when you attempt to serialize the object.

  2. What is the purpose of serialization?

    Answer:
    The purpose of serialization in Java is to convert the state of an object into a byte stream, which can then be saved to a database or transferred over a network. This mechanism enables the persistence and transmission of object state, making it useful for tasks such as saving the state of an object to disk, sending it over a network, or passing it between different parts of a program. The process involves using the ObjectOutputStream and ObjectInputStream classes along with the implementation of marker interfaces Serializable and Externalizable to achieve the desired serialization and deserialization functionalities. It's a fundamental concept in Java programming that allows for the storage or transmission of objects in a format suitable for various applications. Serialization lets you save an object by writing its state to a special type of I/O Stream.
    FileOutputStream fileOutStream = new FileOutputStream(file);
    ObjectOutputStream output = new ObjectOutputStream(fileOutStream);
    



  3. To which types of variables can volatile be applied to ?

    Answer:
    The volatile keyword can only be applied to instance variables. The volatile modifier tells the JVM that a thread accessing the variable must always reconcile its own 1) private copy of the variable with the 2) master copy in memory.

  4. What is true about a static member in Java?

    Answer:
    All static members exist before you ever make a new instance of a class, and there will be only one copy of a static member regardless of the number of instances of that class.

  5. In Java SE 11 and later, where is an enum declaration prohibited?

    Answer:
    In Java SE 11 and later, the Java Language Specification clearly outlines the contexts in which an enum declaration is prohibited. Enumerations, or enums, are a special data type that enable for variables to be a set of predefined constants. While enums are versatile and can be used in various contexts to enhance readability and reliability of code, there are specific locations where their declaration is not allowed:
    1. Inner Classes of Interfaces: Enums cannot be declared as inner classes within an interface. While interfaces can contain inner classes, the restrictive nature of enums and the purpose of interfaces do not align, making such declarations prohibited.
    2. Local Inner Classes: Enums cannot be declared within a method or any block of code. This prohibition extends to any local scope within a method, including conditional blocks, loops, and initialization blocks. Local inner classes are designed for temporary or limited scope usage, and enums, being static by nature, do not fit this use case.
    3. Generic Type Parameters: Enums cannot be used as generic type parameters. While enums can implement interfaces, they cannot be parameterized with generic types due to their static and final nature. This means you cannot create an enum instance that extends or implements a parameterized type with varying type arguments.

    It's important to note that enums can be declared as standalone public enums, package-private enums, inner enums within classes or outer classes, and as private or protected members of classes. However, the restrictions mentioned above are enforced by the Java compiler to maintain the integrity and intended usage of enums within the Java programming language. Understanding these restrictions is crucial for Java developers to ensure that enums are used effectively and in accordance with the language's design principles and constraints, thereby avoiding compilation errors and promoting best practices in Java application development.
    Enums can be declared as their own separate class and they must not be declared within a method. As of 5.0, Java lets you restrict a variable to having one of only a few pre-defined values, in other words, one value from an enumerated list. (The items in the enumerated list are called, surprisingly, enums.)

  6. How would you write a simple enum for the colors RED, BLUE, and GREEN?

    Answer:
    public enum Color{
     RED, BLUE, GREEN;
    }
    

  7. What is the constant type of an enum?

    Answer:
    An enum constant's type is the enum type.

  8. What is an enum in Java?

    Answer:
    A Java Enum is a special Java type used to define collections of constants. A Java enum type is a special kind of Java class and an enum can contain constants, methods.
    The most important thing to remember is that "enums" are not Strings or ints.

  9. What does the enum method values() return?

    Answer:
    Every enum has a static method called "values()", that returns an array of the enum's values in the order they are declared.

  10. Can you invoke an enum constructor directly?

    Answer:
    You can NEVER invoke an enum constructor directly. The enum constructor is invoked automatically, with the arguments you define after the constant value.