Java - Class (Definition)
About
A class provides the blueprint for objects; you create an object from a class. All classes are derived from the Object class.
A class declaration names the class and encloses the class body between braces. The class name can be preceded by modifiers. The class body contains:
- and constructors
for the class.
In the Java programming language, every application start in a class with the help of the main method.
A class uses:
- fields to contain state information
- and uses methods to implement behaviour.
Constructors that initialize a new instance of a class use the name of the class and look like methods without a return type.
Every application begins with a class definition.
A class is a blueprint or prototype from which objects are created.
Within an instance method or a constructor, “this” is a reference to the current object.
Special Type Class:
You control access to classes and members in the same way: by using an access modifier such as public in their declaration.
Articles Related
Declaration
Minimal
The most basic form of a class definition is:
class MyClass {
//field, constructor, and method declarations
}
The keyword class begins the class definition for a class named MyClass and the class body (the area between the braces) contains all the code that provides for the life cycle of the objects created from the class:
- constructors for initializing new objects,
- declarations for the fields that provide the state of the class and its objects,
- and methods to implement the behaviour of the class and its objects.
You can add at the very beginning modifiers like:
- public (other classes can access MyClass)
- or private
Extend
class MyClass extends MySuperClass implements YourInterface { //field, constructor, and method declarations }
means that MyClass is a subclass of MySuperClass and that it implements the YourInterface interface.
Final
You can also declare an entire class final — this prevents the class from being subclassed. This is particularly useful, for example, when creating an immutable class like the String class.
abstract
An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
abstract void moveTo(double deltaX, double deltaY);
CLASSPATH
Java classes needed to execute a Java program can be physically located in different file system directories. When Java classes are loaded by the Java Virtual Machine, it searches different file system directories specified in the CLASSPATH variable to resolve references to the classes needed for execution.
Support
java.lang.NoClassDefFoundError
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp.java.
Check your classpath. Your class cannot be found.
