DEV Community

Abishek
Abishek

Posted on

Java basics like Class,Datatype,Static....

Class

1, what is class?
-> Class is blueprint or template
-> A class is the blueprint from which individual objects are created.
-> This section defines a class that models the state and behavior of a real-world object
-> It intentionally focuses on the basics, showing how even a simple class can cleanly model state and behavior.
-> the word class is a reserved word in java
-> A class has one or more methods.
-> A class should have only one main method
-> A class is indeed a non-primitive data type in programming.

DataType

  • there are two types of data types in java.

    • 1. primitive datatype
    • 2. non-primitive datatype

Primitive datatypes

  • There are two type
    • numeric type
      • byte
      • short
      • int
      • long
      • float
    • non numeric type
      • boolean
      • char

Non-Primitive datatypes

  • Non-primitive data types are called reference types because they refer to objects.

    • String
    • array
    • class
    • object
    • interface
    • enum

the image show that how the data types will be declared,ranges,size...

Static

  • When a member is declared static, it belongs to the class rather than instances of the class.This means that only one instance of the static member exists, regardless of how many objects of the class are created.
  • static is a non-access modifier in Java.

  • Access modifiers: public, private, protected,default.

  • Non-access modifiers: static, final, abstract, synchronized, transient, volatile.

The static keyword can be used with the following:

  • Variable (also known as a class variable)
  • Method (also known as a class method)
  • Block
  • Nested class

static Variable

class Example {
    static int counter = 0;
}
Enter fullscreen mode Exit fullscreen mode

Static Method

class Example {
    static void display() {
        System.out.println("Static method called");
    }
}
Enter fullscreen mode Exit fullscreen mode

Static nested class(TBD)

class OuterClass {
    static class NestedStaticClass {
        void display() {
            System.out.println("Static nested class method called");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Static block(TBD)

public class StaticBlockExample {
    static {
        System.out.println("Static block executed");
    }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)