1. Class (Type, File) Flashcards
Ways to Create a New Type/Class in Java
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.
Inner Classes in Java
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.
Anonymous Inner Class
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.
Multiple Classes in a Single Java File
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.
Naming Convention for Java Classes
Class names in Java should follow the PascalCase convention and should be nouns.
Keywords for Creating New Classes/Types
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.
Structure of a New Type in Java
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.
Java Bytecode
Java bytecode is the instruction set for the Java Virtual Machine (JVM). It is generated during compilation and enables platform independence for Java programs.
Structure of a ‘.class’ File
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.
What is a Class in Java?
A class in Java serves as a blueprint for creating objects. It defines the structure and behavior of objects.
Abstract Class
An abstract class in Java cannot be instantiated. It is declared using the ‘abstract’ keyword and serves as a template for concrete subclasses.
Abstract Class and ‘final’ Modifier
An abstract class cannot be declared as ‘final’ because it’s meant to be extended by subclasses, and declaring it ‘final’ would prevent this.
Abstract Method in Non-Abstract Class
It is a compile-time error to declare an abstract method in a non-abstract class in Java.
Methods in a Class
A class in Java can have up to 65,535 methods.
Constructors in an Abstract Class
Abstract classes can have constructors, even though they cannot be directly instantiated. Constructors in an abstract class may initialize its members.
Interface Inside a Class
Java allows classes to have nested interfaces, which can have various access modifiers, including public, private, or protected.
Records
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.
Shadowing
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.
Purpose of Static and Non-Static Blocks
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.
Static Blocks
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.
Non-Static Blocks
Also known as instance initializers.
Executed when an instance is created.
Cannot contain the return statement.
Difference between Code Blocks and Instance Initializers
Code blocks and instance initializers are not the same.
Instance initializers cannot be inside methods.