DEV Community

muthukumarasamy thangavel
muthukumarasamy thangavel

Posted on

Java Object

What is Object?
** Package name:java.lang.Object**
--> Run time memory allocation.
-->Class Object is the root of the class hierarchy.
-->Every class has Object as a superclass.
--> All objects, including arrays, implement the methods of this class.
-->Everything in Java is associated with classes and objects, along with its attributes and methods.

Example:

 SuperMarket product2 = new  SuperMarket();
 object creation statement
Enter fullscreen mode Exit fullscreen mode

Variables:
-->Variables are containers for storing data values.
-->Variables in Java are used to store data values during program execution. Each variable is associated with a specific data type that defines the kind of value it can hold. Variables help in performing operations, storing user input, and managing data in a program.

1.Local variable -Non static variable
2.Instance variable - Non static variable
3.Global variable -Static variable

Local variable:
-->A variable defined within a block, method, or constructor is referred to as a local variable.
-->Its stored in stack memory.
-->We first need to initialize a local variable before using it within its scope.
-->We first need to initialize a local variable before using it within its scope.
Example:

public static void main(String[] args){
int  age = 20;
String  name = "kumar";
}
Enter fullscreen mode Exit fullscreen mode

Instance variable:

-->Instance variables are known as non-static variables and are declared in a class outside of any method, constructor, or block.
-->Instance variables are created when an object is instantiated and destroyed when the object is destroyed.
-->Can have access specifiers; default access is used if none is specified.
-->Accessed only through objects of the class.
-->Instance Variables can be initialized using constructors or instance blocks.

public class Student{
//Instance Variable
String student_name = "Selvakumar";
int toatal_mark = 420;
    public static void main(String[] args){
        //Local variable 
        int  age = 20;
        String  name = "kumar";
}
}
Enter fullscreen mode Exit fullscreen mode

Static Variable(or) Global variable :
-->Static variables in Java are variables declared with the static keyword inside a class but outside any method.
-->They are shared among all objects of the class and exist for the entire lifetime of the program.
-->There is only one copy of a static variable for the entire class, and all objects share it.
-->Static variable are created at program start and destroyed when the program ends.

public class Student{
//Static variable  with static keyword
static  String  collge_ name = "Excel Eng college";
static int  code = 837;
//Instance Variable
String student_name = "Selvakumar";
int toatal_mark = 420;
    public static void main(String[] args){
        //Local variable 
        int  age = 20;
        String  name = "kumar";
}
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)