Questions and Answers
What are the three structures of a Java class?
What components does a class contain?
Package statement, Import statements, Comments, Class declaration, Variables, Constructors, Methods, Nested classes, Nested interfaces, Enum
What is a package statement in Java?
Defines what package a class is in.
What is an import statement in Java?
Signup and view all the answers
Single line comments in Java can be placed anywhere.
Signup and view all the answers
What is the minimum requirement for a class declaration in Java?
Signup and view all the answers
What defines the class in Java?
Signup and view all the answers
What are instance variables?
Signup and view all the answers
What are class variables?
Signup and view all the answers
What are instance methods?
Signup and view all the answers
What are class methods used for?
Signup and view all the answers
What is the purpose of a constructor in Java?
Signup and view all the answers
What is the structure and components of a Java source code file?
Signup and view all the answers
What is the name of the Java compiler?
Signup and view all the answers
What is an interface in Java?
Signup and view all the answers
You can define multiple classes and interfaces in any order in a single Java source code file.
Signup and view all the answers
You can define multiple classes and interfaces as public in a single source code file.
Signup and view all the answers
If you declare a class or interface as public in a source code file, the file must share the same name.
Signup and view all the answers
What does the import statement import.pkgname.* do?
Signup and view all the answers
Study Notes
Structure of a Java Class
- A Java class consists of three main structures: the class definition (
class Person
), the source code file (Person.java
), and the bytecode (Person.class
). - The class definition is initiated with the
class
keyword followed by the class name and body enclosed in curly braces. - The source code file uses the same name as the class with a
.java
extension and contains the class declaration.
Class Components
- Key components of a Java class include:
- Package statement: denotes the package the class belongs to.
- Import statements: allows the use of other classes/interfaces from different packages without their fully qualified names.
- Comments: provide explanations and can be single-line or multi-line.
- Class declaration: contains variables, constructors, methods, and can include nested classes/interfaces and enums (though not covered in the exam).
Package Statement
- The package statement defines the package a class is assigned to and is mandatory if not using the default unnamed package.
- It must be the first statement in the class and cannot be duplicated; example:
package certification;
.
Import Statement
- The import statement allows access to classes/interfaces from other packages without specifying the full path.
- Must appear after the package statement and before the class definition; example:
import certification.ExamQuestion;
.
Comments
- Single line comments are marked with
//
and can be placed anywhere outside of double-quoted strings.
Class Declaration
- Class declaration, also known as class signature, includes various components as the first line of a class.
- Minimum syntax:
class ClassName { ... }
. More elements like access modifiers and inheritance can be added (e.g.,public final class Runner extends Person
).
Class Definition
- The class is defined by its declaration, encapsulating both attributes and methods.
- Attributes are represented as instance variables that represent the state of an object.
Instance Variables
- Instance variables, or attributes, are defined inside a class but outside methods and represent the state of individual objects.
- Each object maintains its own copy of these variables.
Class Variables
- Class variables, also known as static variables, are defined outside methods and shared across all instances of the class.
- They hold the same value for all objects of that class and cannot be changed independently.
Instance Methods
- Instance methods are non-static and can only be invoked by creating an object of the class in which they reside.
- They typically manipulate instance variables.
Class Methods
- Class methods, also referred to as static methods, operate on class variables and can be called without creating an object.
Constructor
- A constructor initializes objects and shares the same name as the class.
- A class may have multiple constructors with different parameter sets to instantiate objects differently.
Java Source Code File Structure
- Java source code files define one or more Java entities like classes and interfaces, and must end with a
.java
extension. - The source code file can contain package and import statements.
Java Compiler
- The Java compiler is named
javac
on Mac, Linux, and UNIX systems, while on Windows it isjavac.exe
.
Interface Definition
- An interface is a collection of related methods and constants serving as a contract for classes to implement.
Multiple Class and Interface Declarations
- Multiple classes and interfaces can be defined in any order within a single Java source code file, adhering to visibility rules.
Public Class and Interface Naming Rules
- Only one public class or interface is allowed per source file, and if declared public, the file name must match the public entity's name.
Importing Packages
- The syntax
import pkgname.*
is used to import all classes and interfaces from a specified package.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
These flashcards cover the fundamental structures of a Java class and its associated source code file. Learn about the components of a Java class, including class definitions and bytecode conversion. Ideal for beginners in Java programming.