The following code shows an Example of Creating a Class in Java.
A simple example of creating a class in Java is presented here. Class Box has three instance variables, a constructor, and two methods – show(), and volume().
class Box
{
double width, height, depth;
public Box(double width, double height, double depth)
{
this.width=width;
this.height=height;
this.depth=depth;
}
public double volume()
{
return width*height*depth;
}
public void show()
{
System.out.println("Width: "+width);
System.out.println("Height: "+height);
System.out.println("Depth: "+depth);
}
}
public class Main
{
public static void main(String[] args) {
System.out.println("Creating Box Object...");
Box ob=new Box(45, 12.9, 12.6);
ob.show();
System.out.println("Box Volume: "+ob.volume());
}
}
Output
Creating Box Object…
Width: 45.0
Height: 12.9
Depth: 12.6
Box Volume: 7314.3
Further Reading
- AI
- Android
- Angular
- ASP.NET
- Augmented Reality
- AWS
- Bioinformatics
- Biometrics
- Blockchain
- Bootstrap
- C
- C#
- C++
- Cloud Computing
- Competitions
- Courses
- CSS
- Cyber Security
- Data Science
- Data Structures and Algorithms
- Data Visualization
- Datafication
- Deep Learning
- DevOps
- Digital Forensic
- Digital Trust
- Digital Twins
- Django
- Docker
- Dot Net Framework
- Drones
- Elasticsearch
- ES6
- Extended Reality
- Flutter and Dart
- Full Stack Development
- Git
- Go
- HTML
- Image Processing
- IoT
- IT
- Java
- JavaScript
- Kotlin
- Latex
- Machine Learning
- MEAN Stack
- MERN Stack
- Microservices
- MongoDB
- NodeJS
- PHP
- Power Bi
- Projects
- Python
- Quantum Computing
- React
- Robotics
- Rust
- Scratch 3.0
- Shell Script
- Smart City
- Software
- Solidity
- SQL
- SQLite
- Tecgnology
- Tkinter
- TypeScript
- VB.NET
- Virtual Reality
- Web Designing
- WebAssembly
- XML