GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java class - working with classes in Java Java classlast modified January 27, 2024 In this article we show how to work with classes in Java. Java regular classThe class keyword is used do define classes, which are templates for creating objects. The objects are called instances of a class. A new class is created with the new keyword. Inside a class, we define member fields and member functions. The functions defined inside classes are called methods. Member fields and functions are accessed through the dot operator. com/zetcode/RegularClassEx.java
package com.zetcode;
import java.util.Objects;
class User {
private String name;
private String occupation;
public User(String name, String occupation) {
this.name = name;
this.occupation = occupation;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("User{");
sb.append("name='").append(name).append('\'');
sb.append(", occupation='").append(occupation).append('\'');
sb.append('}');
return sb.toString();
}
}
public class RegularClassEx {
public static void main(String[] args) {
var u = new User("John Doe", "gardener");
System.out.println(u);
System.out.println(u.getName());
System.out.println(u.getOccupation());
}
}
In the program, we define the User class. The class has two fields: name and
occupation. In the class, we also define getter and setter methods for the
fields and the class User {
...
}
The private String name; private String occupation;
Two public User(String name, String occupation) {
this.name = name;
this.occupation = occupation;
}
This is the constructor of the class; it is a special method that has the same name as the class. It is called when an instance of the class is created. In our case, we initialize our fields in the constructor. public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
For accessing private fields, we have defined two getter and setter methods. @Override
public String toString() {
final StringBuilder sb = new StringBuilder("User{");
sb.append("name='").append(name).append('\'');
sb.append(", occupation='").append(occupation).append('\'');
sb.append('}');
return sb.toString();
}
To get a string representation of an object, we define the var u = new User("John Doe", "gardener");
A new instance of the System.out.println(u);
The System.out.println(u.getName()); System.out.println(u.getOccupation());
On the user object, we call the $ java com.zetcode.RegularClassEx
User{name='John Doe', occupation='gardener'}
John Doe
gardener
Java abstract classAn abstract class in an unfinished class. It must be implemented in its subclasses. Abstract class is created with the abstract keywords. We can create abstract methods and member fields. The purpose of an abstract class is to provide a common definition for descendant classes. Abstract classes cannot be instantiated. If a class contains at least one abstract method, it must be declared abstract too. Abstract methods cannot be implemented; they merely declare the methods' signatures. com/zetcode/AbstractClassEx.java
package com.zetcode;
abstract class Drawing {
protected int x = 0;
protected int y = 0;
public abstract double area();
public String getCoordinates() {
return String.format("x: %d, y: %d", this.x, this.y);
}
}
class Circle extends Drawing {
private int r;
public Circle(int x, int y, int r) {
this.x = x;
this.y = y;
this.r = r;
}
@Override
public double area() {
return this.r * this.r * Math.PI;
}
@Override
public String toString() {
return String.format("Circle at x: %d, y: %d, radius: %d",
this.x, this.y, this.r);
}
}
public class AbstractClassEx {
public static void main(String[] args) {
Circle c = new Circle(12, 45, 22);
System.out.println(c);
System.out.format("Area of circle: %f%n", c.area());
System.out.println(c.getCoordinates());
}
}
We have an abstract base abstract class Drawing {
We use the public abstract double area();
An abstract method is also preceded with a class Circle extends Drawing {
A @Override
public double area() {
return this.r * this.r * Math.PI;
}
Here we are implementing the $ java com.zetcode.AbstractClass Circle at x: 12, y: 45, radius: 22 Area of circle: 1520.530844 x: 12, y: 45
We create a Java nested classIt is possible to define a class within another class. Such class is called a nested class in Java terminology. A class that is not a nested class is called a top-level class. Java has four types of nested classes:
Using nested classes may increase the readability of the code and improve the organization of the code. Inner classes are often used as callbacks in GUI. For example in Java Swing toolkit. Java static nested classA static nested class is a nested class that can be created without the instance of the enclosing class. It has access to the static variables and methods of the enclosing class. com/zetcode/SNCTest.java
package com.zetcode;
public class SNCTest {
private static int x = 5;
static class Nested {
@Override
public String toString() {
return "This is a static nested class; x:" + x;
}
}
public static void main(String[] args) {
SNCTest.Nested sn = new SNCTest.Nested();
System.out.println(sn);
}
}
The example presents a static nested class. private static int x = 5;
This is a private static variable of the static class Nested {
@Override
public String toString() {
return "This is a static nested class; x:" + x;
}
}
A static nested class is defined. It has one method which prints a message
and refers to the static SNCTest.Nested sn = new SNCTest.Nested(); The dot operator is used to refer to the nested class. $ java com.zetcode.SNCTest This is a static nested class; x:5 Java inner classAn instance of a normal or top-level class can exist on its own. By contrast, an instance of an inner class cannot be instantiated without being bound to a top-level class. Inner classes are also called member classes. They belong to the instance of the enclosing class. Inner classes have access to the members of the enclosing class. com/zetcode/InnerClassTest.java
package com.zetcode;
public class InnerClassTest {
private int x = 5;
class Inner {
@Override
public String toString() {
return "This is Inner class; x:" + x;
}
}
public static void main(String[] args) {
InnerClassTest nc = new InnerClassTest();
InnerClassTest.Inner inner = nc.new Inner();
System.out.println(inner);
}
}
A nested class is defined in the class Inner {
@Override
public String toString() {
return "This is Inner class; x:" + x;
}
}
An InnerClassTest nc = new InnerClassTest(); First, we need to create an instance of the top-level class. Inner classes cannot exist without an instance of the enclosing class. InnerClassTest.Inner inner = nc.new Inner(); Once we have the top-level class instantiated, we can create the instance of the inner class. $ java com.zetcode.InnerClassTest This is Inner class; x:5
This is the output of the Java local classA local class is a special case of an inner class. Local classes are classes that are defined in a block. (A block is a group of zero or more statements between braces.) A local class has access to the members of its enclosing class.
In addition, a local class has access to local variables if they are declared
Local classes cannot be com/zetcode/LocalClassEx.java
package com.zetcode;
public class LocalClassEx {
public static void main(String[] args) {
final int x = 5;
class Local {
@Override
public String toString() {
return "This is Local class; x:" + x;
}
}
Local loc = new Local();
System.out.println(loc);
}
}
A local class is defined in the body of the @Override
public String toString() {
return "This is Local class; x:" + x;
}
A local class can access local variables if they are declared final. Java anonymous classAnonymous classes are local classes that do not have a name. They enable us to declare and instantiate a class at the same time. We can use anonymous classes if we want to use the class only once. An anonymous class is defined and instantiated in a single expression. Anonymous inner classes are also used where the event handling code is only used by one component and therefore does not need a named reference.
An anonymous class must implement an interface or inherit from a class. But the
Since an anonymous class has no name, it is not possible to define a constructor for an anonymous class. Inside the body of an anonymous class we cannot define any statements; only methods or members. com/zetcode/AnonymousClass.java
package com.zetcode;
public class AnonymousClass {
interface Message {
public void send();
}
public void createMessage() {
Message msg = new Message() {
@Override
public void send() {
System.out.println("This is a message");
}
};
msg.send();
}
public static void main(String[] args) {
AnonymousClass ac = new AnonymousClass();
ac.createMessage();
}
}
In this code example, we create an anonymous class. interface Message {
public void send();
}
An anonymous class must be either a subclass or must implement an interface. Our
anonymous class will implement a public void createMessage() {
Message msg = new Message() {
@Override
public void send() {
System.out.println("This is a message");
}
};
msg.send();
}
An anonymous class is a local class, hence it is defined in the body of a method. An anonymous class is defined in an expression; therefore, the enclosing right bracket is followed by a semicolon. SourceIn this article we talked about Java classes. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Java tutorials. |
| 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. |