{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreiczf5xvdsyt5sh6optxebpfajuxruqney7ltwy4sje5cu6s5kkqfq",
"uri": "at://did:plc:25rdn5elo5izoxrmtis34zuk/app.bsky.feed.post/3mpq3cmvxxn22"
},
"coverImage": {
"$type": "blob",
"ref": {
"$link": "bafkreia7mjyeuqyusmfuycq437hdhopp24e7l3yp2yrzqrqjog4mjsbegq"
},
"mimeType": "image/webp",
"size": 71800
},
"path": "/madhavan_g_77/java-rules-for-creating-a-class-data-types-and-static-variables-gao",
"publishedAt": "2026-07-03T07:42:22.000Z",
"site": "https://dev.to",
"tags": [
"beginners",
"java",
"programming",
"tutorial"
],
"textContent": "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.\n\n# Java Rules for Creating a Class\n\nA **class** is a blueprint for creating objects. It defines the properties (variables) and behaviors (methods) of an object.\n\n## Rules for Creating a Java Class\n\n### 1. Class Name Should Start with a Letter\n\nA 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.\n\n**Valid Examples:**\n\n\n\n class Student\n class Employee\n class _Demo\n\n\n**Invalid Example:**\n\n\n\n class 123Student\n\n\n### 2. Use Pascal Case Naming Convention\n\nJava follows the **Pascal Case** naming convention for class names, where the first letter of every word is capitalized.\n\n**Examples:**\n\n * Student\n * EmployeeDetails\n * BankAccount\n\n\n\n### 3. Class Name Should Not Be a Java Keyword\n\nReserved keywords cannot be used as class names.\n\n**Invalid Examples:**\n\n\n\n class int\n class public\n class static\n\n\n### 4. Only One Public Class Per File\n\nIf a class is declared as `public`, the filename must match the class name.\n\nExample:\n\n**File Name:** Student.java\n\n\n\n public class Student {\n }\n\n\n### 5. Curly Braces Define the Class Body\n\nEverything related to the class, such as variables, constructors, and methods, must be enclosed within curly braces `{ }`.\n\nExample:\n\n\n\n public class Student {\n\n int id;\n String name;\n\n void display() {\n System.out.println(name);\n }\n\n }\n\n\n### 6. A Class Can Contain\n\n * Variables\n * Methods\n * Constructors\n * Nested Classes\n * Static Members\n\n\n\n## Example of a Java Class\n\n\n public class Student {\n\n int id;\n String name;\n\n void display() {\n System.out.println(\"ID: \" + id);\n System.out.println(\"Name: \" + name);\n }\n\n public static void main(String[] args) {\n\n Student s = new Student();\n\n s.id = 101;\n s.name = \"Rahul\";\n\n s.display();\n }\n }\n\n\n**Output:**\n\n\n\n ID: 101\n Name: Rahul\n\n\n# Java Data Types\n\nA **data type** specifies the type of value that a variable can store.\n\nJava data types are divided into two categories:\n\n 1. Primitive Data Types\n 2. Non-Primitive (Reference) Data Types\n\n\n\n## 1. Primitive Data Types\n\nPrimitive data types are predefined by Java and store simple values.\n\nData Type | Size | Example\n---|---|---\nbyte | 1 byte | 100\nshort | 2 bytes | 2000\nint | 4 bytes | 50000\nlong | 8 bytes | 9876543210L\nfloat | 4 bytes | 12.5f\ndouble | 8 bytes | 45.78\nchar | 2 bytes | 'A'\nboolean | 1 bit | true\n\n### Integer Example\n\n\n public class IntegerExample {\n\n public static void main(String[] args) {\n\n int age = 25;\n\n System.out.println(age);\n }\n }\n\n\n**Output**\n\n\n\n 25\n\n\n### Double Example\n\n\n public class DoubleExample {\n\n public static void main(String[] args) {\n\n double salary = 45000.75;\n\n System.out.println(salary);\n }\n }\n\n\n**Output**\n\n\n\n 45000.75\n\n\n### Character Example\n\n\n public class CharacterExample {\n\n public static void main(String[] args) {\n\n char grade = 'A';\n\n System.out.println(grade);\n }\n }\n\n\n**Output**\n\n\n\n A\n\n\n### Boolean Example\n\n\n public class BooleanExample {\n\n public static void main(String[] args) {\n\n boolean isPassed = true;\n\n System.out.println(isPassed);\n }\n }\n\n\n**Output**\n\n\n\n true\n\n\n## 2. Non-Primitive Data Types\n\nNon-primitive data types store references to objects rather than actual values.\n\nExamples include:\n\n * String\n * Arrays\n * Classes\n * Interfaces\n * Objects\n\n\n\nExample:\n\n\n\n public class StringExample {\n\n public static void main(String[] args) {\n\n String name = \"John\";\n\n System.out.println(name);\n }\n }\n\n\n**Output**\n\n\n\n John\n\n\n# Static Variable in Java\n\nA **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.\n\nStatic variables are declared using the `static` keyword.\n\n## Syntax\n\n\n class ClassName {\n\n static dataType variableName;\n\n }\n\n\n## Characteristics of Static Variables\n\n * Belong to the class instead of objects.\n * Memory is allocated only once.\n * Shared among all objects.\n * Can be accessed using the class name.\n * Initialized when the class is loaded.\n\n\n\n## Example of a Static Variable\n\n\n public class Student {\n\n int id;\n String name;\n\n static String college = \"ABC College\";\n\n Student(int i, String n) {\n id = i;\n name = n;\n }\n\n void display() {\n System.out.println(id + \" \" + name + \" \" + college);\n }\n\n public static void main(String[] args) {\n\n Student s1 = new Student(101, \"Rahul\");\n Student s2 = new Student(102, \"Priya\");\n\n s1.display();\n s2.display();\n }\n }\n\n\n**Output**\n\n\n\n 101 Rahul ABC College\n 102 Priya ABC College\n\n\nNotice that both students share the same value for the `college` variable because it is declared as `static`.\n\n## Changing the Value of a Static Variable\n\nIf the value of a static variable changes, the updated value is reflected for all objects.\n\n\n\n public class Student {\n\n static String college = \"ABC College\";\n\n public static void main(String[] args) {\n\n System.out.println(college);\n\n college = \"XYZ University\";\n\n System.out.println(college);\n }\n }\n\n\n**Output**\n\n\n\n ABC College\n XYZ University\n\n\n# Difference Between Static and Instance Variables\n\nStatic Variable | Instance Variable\n---|---\nBelongs to the class | Belongs to an object\nShared by all objects | Separate copy for each object\nCreated once | Created whenever an object is created\nAccessed using the class name | Accessed using object references\nDeclared with the `static` keyword | Declared without the `static` keyword",
"title": "Java Rules for Creating a Class, Data Types, and Static Variables"
}