Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Monday, March 10, 2025

Access specifiers, static members, Comments, Data Types, Variables, Operators

1. Access Specifiers

Access specifiers (or access modifiers) in Java define the scope and visibility of classes, methods, constructors, and variables. They provide encapsulation by controlling how members can be accessed outside their defining class.

  • Public

    • Members declared public are accessible from anywhere in the program.

    • Example:

      public class Student { public String name; }
  • Private

    • Members declared private are accessible only within the same class.

    • Example:

      class Student { private int rollNo; }
  • Protected

    • Accessible within the same package and also in subclasses (even if they are in different packages through inheritance).

    • Example:

      class Student { protected String department; }
  • Default (Package-private)

    • If no modifier is specified, members are accessible only within the same package.

    • Example:

      class Student { String college; // default access }

Use Case:

  • public → APIs and libraries.

  • private → Data hiding in OOP.

  • protected → Inheritance.

  • default → Internal package-level communication.


2. Static Members

The static keyword in Java is used for class-level members (variables and methods). Unlike instance members, they belong to the class rather than to objects.

  • Static Variables

    • Shared by all objects of the class.

    • Example:

      class Student { static String college = "IIT Delhi"; }
  • Static Methods

    • Can be invoked without creating an object.

    • Cannot access non-static (instance) variables directly.

    • Example:

      class Student { static void showCollege() { System.out.println("College: IIT Delhi"); } }
  • Static Block

    • Executes once when the class is loaded.

    • Example:

      class Student { static { System.out.println("Static block executed"); } }

Use Case: Useful for constants, utility methods (Math.sqrt()), and memory management.


3. Comments

Comments are non-executable statements used to increase readability and maintainability of code.

  • Single-line comment (//)

    // This is a single-line comment
  • Multi-line comment (/* */)

    /* This is a multi-line comment */
  • Documentation comment (/** */)

    • Used to generate documentation using Javadoc.

    /** * This class represents a Student entity */ class Student { }

4. Data Types

Java is a strongly typed language, meaning each variable must be declared with a data type.

  • Primitive Data Types (8 types)

    • Integer types: byte, short, int, long

    • Floating types: float, double

    • Character type: char

    • Boolean type: boolean

    Example:

    int age = 25; double salary = 50000.75; char grade = 'A'; boolean isPass = true;
  • Non-Primitive Data Types

    • Classes, Interfaces, Arrays, Strings, Objects.

    • Example:

      String name = "Krishna"; int[] marks = {85, 90, 95};

5. Variables

A variable is a named memory location used to store data during program execution.

  • Types of Variables in Java

    • Local Variables → Declared inside methods, accessible only within that block.

    • Instance Variables → Declared inside class but outside methods; unique for each object.

    • Static Variables → Declared with static, common for all objects.

Example:

class Student { int rollNo; // instance variable static String college = "IIT"; // static variable void display() { String subject = "Math"; // local variable } }

6. Operators

Operators are special symbols used to perform operations on variables and values.

  • Arithmetic Operators: + - * / %

  • Relational Operators: == != > < >= <=

  • Logical Operators: && || !

  • Assignment Operators: = += -= *= /= %=

  • Unary Operators: ++ -- + - ~ !

  • Bitwise Operators: & | ^ ~ << >> >>>

  • Ternary Operator: condition ? expr1 : expr2

Example:

int a = 10, b = 20; System.out.println(a + b); // Arithmetic System.out.println(a > b); // Relational System.out.println(a != b && a < b); // Logical

Summary in Academic Tone:

Access specifiers regulate encapsulation, static members ensure class-level data sharing, comments enhance documentation, data types provide strict type safety, variables facilitate memory allocation, and operators allow computations. Together, these form the fundamental building blocks of Java programming, ensuring modularity, maintainability, and efficiency.

No comments:

Post a Comment