#IT
63 Views

pls explain me static varaible in java not at all understanding it ??


Akanksha 21st Oct, 2020
Answers (3)
Charu tyagi 21st Oct, 2020

hello aspirant

basically in java, Static variable is used for some common properties of the objects example- school name  of students, college name etc.it is common to all and not at all unique.it is stored in static memory(heap memory). and can be called by object.

it makes program easy to understandable and effecient.it  manages memory. In this a copy of variable is created and shared. it is among the class level so it is same for all the instamces of the class.These are globally acceptable.

Syntax of static variable is :- static data_type variable_name;

It is a very good memory management technique.

you can refer to the following example for better understanding and declaration

class Understand;

{ static string name;

static int address;

static int age;

//static method

static void disp(){

System.out.println("name is:"+name);

System.out.println("address is:"+address);

System.out.println("age is:"+age);

}

// static method

public static void main(String []args)

{ name= "miss";

address= street no 2, ghaziabad;

age= 40;

disp();

}

}

hope you understand


NAVYA BOINPALLY Student Expert 21st Oct, 2020

Hi,

The static variable is used to refer to the common property of all objects

for example: The company name of employees, college name of students etc. Static variables gets memory only once in the class area at the time of class loading. Using a static variable makes your program more memory efficient. Static variable belongs to the class rather than the object.

Hope this information is helpful

Thankyou.

Saunkita Mallick 21st Oct, 2020

Hello, hope you are doing well!

Static Variables in Java-

  • Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.

  • There would only be one copy of each class variable per class, regardless of how many objects are created from it.

  • Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final, and static. Constant variables never change from their initial value.

  • Static variables are stored in the static memory. It is rare to use static variables other than declared final and used as either public or private constants.

  • Static variables are created when the program starts and destroyed when the program stops.

  • Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class.

  • Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values can be assigned during the declaration or within the constructor. Additionally, values can be assigned in special static initializer blocks.

  • Static variables can be accessed by calling with the class name ClassName.VariableName.

  • When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables are not public and final, the naming syntax is the same as instance and local variables.

Related Questions

UPES Integrated LLB Admission...
Apply
Ranked #28 amongst Institutions in India by NIRF | Ranked #1 in India for Academic Reputation by QS University Rankings | 16.6 LPA Highest CTC
Jindal Global Law School Admi...
Apply
Ranked #1 Law School in India & South Asia by QS- World University Rankings | Merit cum means scholarships | Application Deadline: 30th Nov'24
Nirma University Law Admissio...
Apply
Grade 'A+' accredited by NAAC
Great Lakes PGPM & PGDM 2025
Apply
Admissions Open | Globally Recognized by AACSB (US) & AMBA (UK) | 17.3 LPA Avg. CTC for PGPM 2024 | Application Deadline: 1st Dec 2024
ICFAI Business School-IBSAT 2024
Apply
9 IBS Campuses | Scholarships Worth Rs 10 CR
UPES B.Tech Admissions 2025
Apply
Ranked #42 among Engineering colleges in India by NIRF | Highest CTC 50 LPA , 100% Placements
View All Application Forms

Download the Careers360 App on your Android phone

Regular exam updates, QnA, Predictors, College Applications & E-books now on your Mobile

150M+ Students
30,000+ Colleges
500+ Exams
1500+ E-books