Java - Class (Definition)

Java Conceptuel Diagram

About

A class provides the blueprint for objects; you create an object from a class. All classes are derived from the Object class.

Simple 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:

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 (the main class) with a main method.

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.

You control access to classes and members in the same way: by using an access modifier such as public in their declaration.

Special Type Class

Name

A class name must be a binary name as defined by The Java™ Language Specification.

Examples of valid class names include:

  • “java.lang.String”
  • “javax.swing.JSpinnerDefaultEditor”
  • “java.security.KeyStoreBuilderFileBuilder1”
  • “java.net.URLClassLoader31”

With the name following the Pascal Casing. (ie wiki/CamelCase which always begins with a capital letter)

Literal

Below string class is a class literal.

Class<String> c = String.class;

A class literal can be passed among methods to communicate both:

  • compile-time
  • and runtime type information (Used intensively with reflection)

In this case, it is called a type token

See also: Design Pattern - Typesafe heterogeneous container (Java)

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.

Implement

Java - Implements clause

Modifier

Load

See Java - Class (Loader|Loading) Process

Main

A start or main class is the first entry point of a whole application. She must have a main method in order to start the application. She is defined in the manifest file.

version

For the java version that have compiled the class, see Java - Compile (class file)

Type

Enclosing

An enclosing class is a static inner class

Instantiation

For every type of object, the Java virtual machine instantiates an immutable instance of java.lang.Class which:

  • provides methods to examine the runtime properties of the object (including its members and type information)
  • provides the ability to create new classes and objects.
  • is the entry point for all of the Reflection APIs.

Management

Dependency

See Java - Dependency (of a class)

a class when an instance of an object is available

The simplest way is to invoke Object.getClass().

the class

String

For instance, this statement will returns the Class for String in the Class nicoStringClass .

Class nicoStringClass = "nico".getClass();

Console

Class c = System.console().getClass();

There is a unique console associated with the virtual machine which is returned by the static method System.console(). The value returned by getClass() is the Class corresponding to java.io.Console.

Enumeration

 
enum E { A, B }
Class c = A.getClass();

A is is an instance of the enum E; thus getClass() returns the Class corresponding to the enumeration type E.

Array

 
byte[] bytes = new byte[1024];
Class c = bytes.getClass();

Since arrays are Objects, it is also possible to invoke getClass() on an instance of an array. The returned Class corresponds to an array with component type byte.

Support

java.lang.NoClassDefFoundError

Exception in thread "main" java.lang.NoClassDefFoundError:
HelloWorldApp.java.

Check your classpath. Your class cannot be found.

Unsupported major.minor version

Exception in thread "main" java.lang.UnsupportedClassVersionError: myClass: Unsupported major.minor version 51.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: myClass.  Program will exit.

be sure to use the good JRE Version.

Documentation / Reference





Discover More
Java Conceptuel Diagram
J2EE - Class Redefinition (FastSwap)

Java EE 5 introduces the ability to redefine a class at runtime without: dropping its ClassLoader or abandoning existing instances. This allows containers to reload altered classes without disturbing...
Jdbc Class Architecture
JDBC - Oracle

Definition of the JDBC Oracle API or how to connect to a JDBC oracle and made database operations. A simple application includes the following classes: DataHandler.java. This class contains all...
Java Conceptuel Diagram
JSF - Listener

Listeners are used to listen to the events happening in the page and perform actions as defined. An application developer can implement listeners as: classes or as managed bean methods. If...
Java Conceptuel Diagram
Java

Why has become so popular among application developers? Primarily because makes application developers more productive. It is a modern, robust, object-oriented language. ’s unprecedented popularity...
Java Conceptuel Diagram
Java - (Data Type|Type)

data type in the java world. The Java programming language is statically-typed, which means that all variables must first be declared before they can be used. The language supports four kinds...
Java Conceptuel Diagram
Java - (Generic|Parameterized) type - (Class|Interface|Method) Parametrization

and Integer.class is of type Class Stronger type checks at compile time. Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find. The compiler can check the type...
Java Conceptuel Diagram
Java - (Jar|Java ARchive) File

JAR stands for Java ARchive and is file format born in 1996 as a simple extension of the popular ZIP archive format with class and other required resource files: manifest signature files images,...
Eclipse Main Class
Java - (Main|Start) Class

in Java is called a Main class. The (Main|Start) class is a class that: have a main method will start the main thread (process) main classjar You can start the java application by: calling...
Java Conceptuel Diagram
Java - API - Class Library

Classes, interfaces, constructors, members, and serialized forms are collectively known as API elements. A class whose implementation uses an API is a client of the API. An exported API consists of the...
Classes Access
Java - Access Modifier (private, public, )

in Java. Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control: At the top level: public, or package-private...



Share this page:
Follow us:
Task Runner