1. Class (Type, File) Flashcards

1
Q

Ways to Create a New Type/Class in Java

A

To create a new type or class in Java, we need to declare it. There are three types of class declarations: normal class declarations, enum declarations, and record declarations. A normal class declaration includes class modifiers, type identifier, type parameters (if any), class extends, class implements, class permits, and the class body.

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

Inner Classes in Java

A

Java defines three types of inner classes: member inner classes, local inner classes, and anonymous inner classes. Member inner classes are classes defined at the member level of another class and can access the members of the outer class, including private ones. They can have various access modifiers, be abstract or final, extend classes, implement interfaces, and declare static fields or methods.

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

Anonymous Inner Class

A

An anonymous inner class is an inner class that lacks a name and is declared and instantiated in one statement using the new keyword, type name with parentheses, and braces {}. It must extend an existing class or implement an existing interface. It is useful for short implementations that won’t be used elsewhere.

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

Multiple Classes in a Single Java File

A

A single Java file can contain multiple classes, but only one of them can have a public access modifier. Other classes in the same file must not have the public access modifier.

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

Naming Convention for Java Classes

A

Class names in Java should follow the PascalCase convention and should be nouns.

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

Keywords for Creating New Classes/Types

A

To create new classes or types in Java, you can use class modifiers, including Annotation, public, private, abstract, static, final, sealed, non-sealed, and strictfp.

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

Structure of a New Type in Java

A

A Java class can include various structural elements, including fields, methods, nested classes, nested interfaces, or nested enum types. Additionally, a class body may contain instance initializers, static initializers, and constructor declarations.

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

Java Bytecode

A

Java bytecode is the instruction set for the Java Virtual Machine (JVM). It is generated during compilation and enables platform independence for Java programs.

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

Structure of a ‘.class’ File

A

A ‘.class’ file consists of 8-bit bytes and contains the definition of a single class, interface, or module. It includes various components such as magic, minor_version, major_version, constant_pool_count, constant_pool, access_flags, and more.

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

What is a Class in Java?

A

A class in Java serves as a blueprint for creating objects. It defines the structure and behavior of objects.

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

Abstract Class

A

An abstract class in Java cannot be instantiated. It is declared using the ‘abstract’ keyword and serves as a template for concrete subclasses.

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

Abstract Class and ‘final’ Modifier

A

An abstract class cannot be declared as ‘final’ because it’s meant to be extended by subclasses, and declaring it ‘final’ would prevent this.

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

Abstract Method in Non-Abstract Class

A

It is a compile-time error to declare an abstract method in a non-abstract class in Java.

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

Methods in a Class

A

A class in Java can have up to 65,535 methods.

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

Constructors in an Abstract Class

A

Abstract classes can have constructors, even though they cannot be directly instantiated. Constructors in an abstract class may initialize its members.

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

Interface Inside a Class

A

Java allows classes to have nested interfaces, which can have various access modifiers, including public, private, or protected.

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

Records

A

Records in Java are a specialized kind of class that represents a simple aggregate of values. They have a compact syntax for defining data classes.

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

Shadowing

A

Shadowing occurs when a declaration of a type in a particular scope has the same name as another declaration in the enclosing scope, leading to the shadowed declaration not being accessible by its name alone.

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

Purpose of Static and Non-Static Blocks

A

Static blocks are used for class initialization and execute when the class is loaded.
Non-static blocks (instance initializers) run when an instance of the class is created.

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

Static Blocks

A

Defined using the static keyword.
Multiple static blocks execute in the order they are declared.
Cannot contain the return statement or reference variables outside the block.

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

Non-Static Blocks

A

Also known as instance initializers.
Executed when an instance is created.
Cannot contain the return statement.

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

Difference between Code Blocks and Instance Initializers

A

Code blocks and instance initializers are not the same.
Instance initializers cannot be inside methods.

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

Life Cycle of Static and Non-Static Blocks

A

Static Blocks:

Run when the class is first used, either through object creation or accessing static members.
Execute in the order they are defined.
Assign static variables as needed.
Non-Static Blocks:

Run in the order they appear in the file.
Execute before the constructor.

24
Q

Static and Non-Static Blocks in Inheritance

A

Execution Rules:
Parent class’s static initializer runs when any method in its subclass is executed.
Parent class’s static initializer doesn’t run if only a method in the parent class is called.
Parent’s instance initializer runs when a parent class instance is created inside a method being executed.
Order when child class object is first created: Parent static block → Child static block → Parent instance block → Parent constructor → Child instance block → Child constructor.

25
Q

Usage of Static Blocks and Best Practices

A

Static Blocks Usage:
Typically used for initializing static fields with complex logic.
Commonly seen when initializing collections like ArrayList.
Recommended to consolidate all static initialization within a single block for clarity.

26
Q

Non-Static Blocks Usage and Best Practices

A

Non-Static Blocks Usage:
Use instance initializers when logic cannot be handled by constructors.
Double brace initialization pattern can be used for initializing collections.

27
Q

Classloading Mechanism in Java

A

ClassLoader Phases:
ClassLoader is responsible for loading .class files into memory.
ClassLoader has three phases: loading, linking, and initialization.

28
Q

Default ClassLoader Hierarchy

A

ClassLoader Hierarchy:
Class loaders follow a delegation hierarchy.
Bootstrap class loader (primordial), Extension class loader, and Application class loader.

29
Q

Roles of ClassLoaders in Hierarchy

A

Roles in the Hierarchy:
Bootstrap ClassLoader: Loads classes from rt.jar.
Extension ClassLoader: Loads extensions of core Java classes.
System (Application) ClassLoader: Loads application-specific classes from CLASSPATH.

30
Q

Importance of Multiple ClassLoaders

A

Why Use Multiple ClassLoaders:
All class loaders are essential for Java applications to function correctly.
Removing any class loader would disrupt the hierarchy.

31
Q

Life Cycle of a Class Loaded by ClassLoaders

A

Class Loading Life Cycle:
Delegation model: System ClassLoader → Extension ClassLoader → Bootstrap ClassLoader.
ClassNotFoundException if not found in any loader.
Life cycle stages: Loading, Linking, Initialization, Instance Creation, Finalization, Unloading.

32
Q

Stages of Class Construction

A

Stages of Class Construction:
Loading: Producing binary data, parsing, creating java.lang.Class instance.
Linking: Verification, preparation, and optionally resolution.
Initialization: Initializing class and its superclass.
Unloading: Removing classes from memory.

33
Q

User-Defined ClassLoaders

A

User-Defined ClassLoaders:
Used to load classes dynamically during runtime.
Enables loading classes from various sources.
Provides flexibility in class loading.

34
Q

Resolving Library Version Conflicts

A

Library Version Conflicts Resolution:
Custom ClassLoaders can load and run multiple versions of the same class.
Useful for resolving conflicts when libraries depend on different versions of a JAR.

35
Q

Loading Different Versions of the Same Libraries

A

Loading Different Versions of Libraries:
Achieved through custom (user-defined) ClassLoaders.
Default ClassLoader loads the same class only once.

36
Q

Exceptions Thrown by Classloading Mechanism

A

Exceptions Thrown:
ClassNotFoundException
LinkageError subclasses: ClassCircularityError, ClassFormatError, NoClassDefFoundError, OutOfMemoryError, IllegalAccessError, NoSuchMethodError, InstantiationError, NoSuchFieldError, UnsatisfiedLinkError, UnsupportedClassVersionError.

37
Q

Best Practices for Custom ClassLoaders

A

Best Practices:
Based on Joshua Bloch’s article.
Maintain class isolation.
Ensure proper delegation to parent loaders.

38
Q

Framework Using Custom ClassLoaders

A

Framework Using Custom ClassLoaders:
Spring Framework.

39
Q

Why Spring Framework Uses Custom ClassLoaders

A

Reasons for Spring’s Custom ClassLoaders:
Enhanced security among other reasons.

40
Q

Extending Bootstrap ClassLoader

A

Extending Bootstrap ClassLoader:
Yes, it’s possible to extend the bootstrap classloader.

41
Q

Class Loading Lifecycle:

A

Three phases: Loading, Linking, and Initializing.

42
Q

Storage of Class Text Representation:

A

In memory cache.

43
Q

Order of Creation:

A

NewClass instance is created first, followed by MyInterface.

44
Q

Importing the Same Class Twice:

A

Yes, it’s allowed, but the compiler loads the class only once.

45
Q

CLASSPATH Meaning:

A

Parameter specifying where the JVM should look for classes and packages.

46
Q

Run-Time Constant Pool:

A

Maintained for each class and interface in the JVM.
Contains two types of entries: symbolic references and static constants.

47
Q

JVM Startup process

A

Starts by creating an initial class or interface using the bootstrap class loader or a user-defined class loader.
Links, initializes, and invokes the public static void main(String[]) method.
Further execution is driven by the main method.

48
Q

Creation of a Class or Interface:

A

Involves constructing an implementation-specific internal representation in the method area.

49
Q

Loading Process:

A

Finding the class file representing a class or interface.
Reading it into a byte array.
Parsing the bytes for correctness.
Creating a class or interface object from the binary representation.

50
Q

Class Loading

A

Class Loading Process:
JVM searches for the class in a specific order: bootstrap class loader, extension class loader, application class loader.
Class is loaded when found; otherwise, ClassNotFoundException is thrown.
Loaded classes are tracked by the class loader used.

51
Q

Linking Process:

A

Incorporating binary type data into the runtime state.
Consists of verification, preparation, and optionally, resolution.
Verification:

Ensures structural correctness and semantic requirements.
Checks include symbol table consistency, method access control, parameter types, bytecode stack manipulation, etc.
Flashcard 6: Preparation and Initialization

Preparation:

Allocating memory for static storage and JVM data structures.
Initializing static fields to their default values.
Initialization:

Executing a class or interface initialization method if present (named <clinit>).
Initialization order involves superclass initialization.</clinit>

52
Q

Dynamic Linking:

A

Resolving symbolic references into direct references.
Symbolic references are replaced with direct references to classes, fields, or methods.
The virtual machine caches direct references for efficiency.

53
Q

Class Loading Optimization:

A

Class loaders can load types early in anticipation of use.
Problems during early loading are reported upon the type’s first active use.

54
Q

Memory Areas in JVM

A

Memory Areas:

JVM divides memory into runtime data areas.
Shared areas include method area and heap, while others may vary between threads.
Namespace Control:

Java’s architecture enables multiple namespaces within a single application through class loader isolation.

55
Q

Class Loaders and Security

A

Class Loaders and Security:

Class loaders define namespaces, protecting Java’s security features.
They interact with the security manager for permission checks.
Class loaders set up permissions for class objects (protection domains).
Custom Class Loaders for Security:

Custom class loaders can establish different security policies for an application.