GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||
GeezerVison is showing jenkov.com — Java Classes Java Language
Java Classes
Java classes are some of the core building blocks of Java applications, toolkits, frameworks, APIs etc.
A small Java application may consist of a single Java class with a A Java class is a single, coherent unit of Java code which belongs together. A Java class may contain a mix of data (variables) and actions (methods). Grouping variables and operations on these variables into Java classes makes it easier to structure your Java program when it gets too big to fit comfortably inside a single Java class. A Java class must be stored in its own file. Therefore, as the class grows, the file you are editing grows too, and becomes harder to keep an overview of in your head. Your Java application will typically have to contain at least a single Java class, but it may contain as many classes as you see fit to divide your application into. Java also comes with a lot of predefined classes for you, so you don't have to code every little function you might desire yourself. Java Class Building BlocksA Java class can contain the following building blocks:
Fields are variables (data) that are local to the class, or instances (objects) of that class. I will get back to instances later. Fields are covered in more detail in my Java fields tutorial. Constructors are methods that initialize an instance of the class. Constructors often sets the values of fields in the given instance. Constructors are covered in more detail in my Java constructors tutorial. Methods are operations that the class or instances of that class can perform. For instance, a method may perform an operation on input parameters, or change the value of fields kept internally in the object etc. Methods are covered in more detail in my Java methods tutorial Nested classes are Java classes that are defined inside another class. Nested classes are typically intended to either be used only internally be the Java class that contains them, or to be used in connection with the class that contains them. Nested classes are covered in more detail in my Java nested class tutorial. Not all Java classes have fields, constructors and methods. Sometimes you have classes that only contain fields (data), and sometimes you have classes that only contain methods (operations). It depends on what the Java class is supposed to do. Defining a Class in JavaAll it takes to define a class in Java is this: public class MyClass {
}
This defines a public Java class called .java Files
The above class definition should be put in its own file named Only put a single class definition in each Java file, unless your class contains inner classes of some kind. Inner classes are covered in my Java nested classes tutorial. Class With FieldsAs mentioned earlier, a Java class can contain data in the shape of variables. Variables that belong to the class are typically called "fields".
The next example shows a Java class which is to model a car. Therefore the class has named public class Car {
public String brand = null;
public String model = null;
public String color = null;
}
This code defines a Java class named Car. The Car class has three fields. The Car class has no methods. Only field declarations. Fields are described in more detail in the text on Java fields. Class With ConstructorA Java class can have a constructor. A constructor is a special method that is called when an object of the given class is created (explained later). The purpose of a constructor is to initialize the fields in the class. The fields are also called the "internal state". Here is an example of a Java class with two constructors: public class Car {
public String brand = null;
public String model = null;
public String color = null;
public Car() {
}
public Car(String theBrand, String theModel, String theColor) {
this.brand = theBrand;
this.model = theModel;
this.color = theColor;
}
}
The constructors are the two methods that have the same name as the class, and which have no return type specified. The first constructor takes no parameters, and the second takes 3 parameters. The constructor that takes 3 parameters stores the values of these parameters in the fields of the created object. Constructors are covered in more detail in my Java constructors tutorial . Class With MethodsA Java class can also contain operations. These operations are typically called methods. A Java method contains Java instructions that typically perform some operations on a field in the class, or on one of the parameters (also variables) values passed to the method when the method was called.
Here is the Java class, public class Car {
public String brand = null;
public String model = null;
public String color = null;
public void setColor(String newColor) {
this.color = newColor;
}
}
In the class definition above I have added a Class With Nested ClassAs mentioned earlier, you can define a nested class inside another Java class. Here is an example of defining a nested class inside a Java class: public class MyClass {
public static class MyNestedClass{
}
}
In the example above, the outer class is called Classes and Objects
A Java class is a template for how objects of that class looks. In other words, the
To create objects of a certain class, you use the Car car1 = new Car();
Car car2 = new Car();
Car car3 = new Car();
car1.setColor("red");
car2.setColor("green");
car3.setColor("blue");
This example creates 3
After creating the 3
Creating an object of a certain class is also called "instantiating" an object. The object is thus also called
an "instance" of the given class. For instance, each of the Further Concepts for Java ClassesWhat you have seen in this text only covers the very basics of Java classes. You need to learn about fields, constructors, methods nested classes, abstract classes, inheritance, access modifiers and interfaces too. All of these concepts are discussed in their own texts. Classes in Other Programming LanguagesClasses is a common concept in many programming languages. Here are links to the tutorials about classes in programming languages covered on this website: Next: Java Fields
Close TOC
All Trails Trail TOC Page TOC Previous Next | ||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. | ||||||