Java is one of the most popular object-oriented programming languages used for developing desktop applications, web applications, mobile apps, and enterprise software. To write efficient Java programs, it is essential to understand the basic concepts such as creating a class, using different data types, and working with static variables. This article explains these concepts in a simple and easy-to-understand manner.
Java Rules for Creating a Class
A class is a blueprint for creating objects. It defines the properties (variables) and behaviors (methods) of an object.
Rules for Creating a Java Class
1. Class Name Should Start with a Letter
A Java class name must begin with an alphabet (A-Z or a-z), underscore (_), or dollar sign ($). However, it is recommended to start with a letter.
Valid Examples:
class Student
class Employee
class _Demo
Invalid Example:
class 123Student
2. Use Pascal Case Naming Convention
Java follows the Pascal Case naming convention for class names, where the first letter of every word is capitalized.
Examples:
- Student
- EmployeeDetails
- BankAccount
3. Class Name Should Not Be a Java Keyword
Reserved keywords cannot be used as class names.
Invalid Examples:
class int
class public
class static
4. Only One Public Class Per File
If a class is declared as public, the filename must match the class name.
Example:
File Name: Student.java
public class Student {
}
5. Curly Braces Define the Class Body
Everything related to the class, such as variables, constructors, and methods, must be enclosed within curly braces { }.
Example:
public class Student {
int id;
String name;
void display() {
System.out.println(name);
}
}
6. A Class Can Contain
- Variables
- Methods
- Constructors
- Nested Classes
- Static Members
Example of a Java Class
public class Student {
int id;
String name;
void display() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
}
public static void main(String[] args) {
Student s = new Student();
s.id = 101;
s.name = "Rahul";
s.display();
}
}
Output:
ID: 101
Name: Rahul
Java Data Types
A data type specifies the type of value that a variable can store.
Java data types are divided into two categories:
- Primitive Data Types
- Non-Primitive (Reference) Data Types
1. Primitive Data Types
Primitive data types are predefined by Java and store simple values.
| Data Type | Size | Example |
|---|---|---|
| byte | 1 byte | 100 |
| short | 2 bytes | 2000 |
| int | 4 bytes | 50000 |
| long | 8 bytes | 9876543210L |
| float | 4 bytes | 12.5f |
| double | 8 bytes | 45.78 |
| char | 2 bytes | 'A' |
| boolean | 1 bit | true |
Integer Example
public class IntegerExample {
public static void main(String[] args) {
int age = 25;
System.out.println(age);
}
}
Output
25
Double Example
public class DoubleExample {
public static void main(String[] args) {
double salary = 45000.75;
System.out.println(salary);
}
}
Output
45000.75
Character Example
public class CharacterExample {
public static void main(String[] args) {
char grade = 'A';
System.out.println(grade);
}
}
Output
A
Boolean Example
public class BooleanExample {
public static void main(String[] args) {
boolean isPassed = true;
System.out.println(isPassed);
}
}
Output
true
2. Non-Primitive Data Types
Non-primitive data types store references to objects rather than actual values.
Examples include:
- String
- Arrays
- Classes
- Interfaces
- Objects
Example:
public class StringExample {
public static void main(String[] args) {
String name = "John";
System.out.println(name);
}
}
Output
John
Static Variable in Java
A static variable is a variable that belongs to the class rather than to individual objects. Only one copy of the static variable is created, and it is shared among all objects of the class.
Static variables are declared using the static keyword.
Syntax
class ClassName {
static dataType variableName;
}
Characteristics of Static Variables
- Belong to the class instead of objects.
- Memory is allocated only once.
- Shared among all objects.
- Can be accessed using the class name.
- Initialized when the class is loaded.
Example of a Static Variable
public class Student {
int id;
String name;
static String college = "ABC College";
Student(int i, String n) {
id = i;
name = n;
}
void display() {
System.out.println(id + " " + name + " " + college);
}
public static void main(String[] args) {
Student s1 = new Student(101, "Rahul");
Student s2 = new Student(102, "Priya");
s1.display();
s2.display();
}
}
Output
101 Rahul ABC College
102 Priya ABC College
Notice that both students share the same value for the college variable because it is declared as static.
Changing the Value of a Static Variable
If the value of a static variable changes, the updated value is reflected for all objects.
public class Student {
static String college = "ABC College";
public static void main(String[] args) {
System.out.println(college);
college = "XYZ University";
System.out.println(college);
}
}
Output
ABC College
XYZ University
Difference Between Static and Instance Variables
| Static Variable | Instance Variable |
|---|---|
| Belongs to the class | Belongs to an object |
| Shared by all objects | Separate copy for each object |
| Created once | Created whenever an object is created |
| Accessed using the class name | Accessed using object references |
Declared with the static keyword |
Declared without the static keyword |
Top comments (0)