Java Classes
In Java, classes are blueprints or templates for objects in Java. They detail the general structure and data for an object including information such as properties, attributes, and method behavior. Classes are classified as a reference data type.
Syntax
accessModifier class ClassName {
dataType attributeOne;
dataType attributeTwo;
dataType attributeN;
static void classMethod {
// Method code here
}
}
- Class names must always be in “PascalCase” and match the name of the file (e.g.
ClassName.java). - Java uses the
classkeyword for creating classes. - They use an
accessModifier(public,private, andprotected) to determine its visibility to other files. - Inside the class “blueprint” are members.
Class Instances
In Java, instances are objects that are based on existing classes.
Every instance has access to its own set of variables known as instance fields. These are variables declared within the scope of the instance and supplied with new values within the class constructor method during initialization.
For example, Bob and Alice may each be defined as instances of the class called Person with the new keyword:
// Person.javapublic class Person {int age;String name;// Constructor methodpublic Person(int age, String name) {this.age = age;this.name = name;}public static void main(String[] args) {Person Bob = new Person(31, "Bob");Person Alice = new Person(27, "Alice");System.out.println(Bob.name + " is " + Bob.age + ".");System.out.println(Alice.name + " is " + Alice.age + ".");}}
Each instance of the Person class has an age and name field. When initialized, they are passed as arguments into the class constructor. The example from above would return the following output:
Bob is 31.Alice is 27.
Abstract Classes
Classes can also use the abstract keyword to supply common method implementations to multiple subclasses. Any class that contains abstraction (methods, fields, etc.) must also be abstract:
// Person.javaabstract class Person {int age;String name;abstract void talk(String message);}
All contributors
- Anonymous contributor
- garanews
BrandonDusch
Victoria-DR
christian.dinh- Anonymous contributor
vlrnsnk
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours