Java Class
In this chapter you will learn:
Description
A class defines a new data type.
This new type can be used to create objects of that type.
A class is a template for an object, and an object is an instance of a class.
Syntax
The general form of a class definition is shown here:
class classname {
type instance-variable1; // www.j a v a2 s .c o m
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list) {
// body of method
}
type methodname2(parameter-list) {
// body of method
}
// ...
type methodnameN(parameter-list) {
// body of method
}
}
A class is declared by using the class keyword.
The methods and variables defined within a class are called class members.
Variables defined within a class are called instance variables because each instance of the class contains its own copy of these variables.
The data for one object is separate and unique from the data for another.
Example
Here is a class called Box
that defines three member variables:
width
, height
, and depth
.
class Box { /* ww w . ja va2 s .c o m*/
int width;
int height;
int depth;
}
Next chapter...
What you will learn in the next chapter:
- Create objects from class
- Syntax to create objects
- Example - Create Java object
- Example - use a null mybox will result in a compile-time error
Java Class
Java ObjectJava Object Reference Variable
Java Methods
Java Method Return
Java Method Parameters
Java Class Constructors
Java Default Constructor
Java Constructor Parameters
Java this Keyword
Java static keyword
Java Method Overload
Java Constructors Overload
Java Method Argument Passing
Java Method Recursion
Java Nested Class
Java Anonymous Classes
Java Local Classes
Java Member Classes
Java Static Member Classes
Java Class Variables
Java main() Method
Java Class Inheritance
Java super keyword
Java Method Overriding
Java Constructor in hierarchy
Polymorphism
Java final keyword
Java Abstract class
Java Class Access Control
Java Package
Java Packages Import
Java Interface
Java Interface as data type
Java interface as build block
Java instanceof operator
Java Source Files