0% found this document useful (0 votes)
14 views3 pages

Discussion Assingment Unit. 3

Static variables in Java are class-level variables shared among all objects of a class, defined using the "static" keyword and stored in the class area. Non-static variables in Java, also referred to as instance variables, are defined within a class without the static keyword and are associated with individual instances of a class. The document provides examples and use cases of static and non-static variables in Java.

Uploaded by

Abdullah Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Discussion Assingment Unit. 3

Static variables in Java are class-level variables shared among all objects of a class, defined using the "static" keyword and stored in the class area. Non-static variables in Java, also referred to as instance variables, are defined within a class without the static keyword and are associated with individual instances of a class. The document provides examples and use cases of static and non-static variables in Java.

Uploaded by

Abdullah Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Static Variables in Java

Static variables in Java are class-level variables shared among all objects of a class, defined using the
"static" keyword and stored in the class area. As they are shared, any object of the class can access
and modify their values. Static variables, also referred to as class variables, are commonly used to
represent class-level data like the count of class instances, maximum property values, or default
property values. They are initialized once when the class is loaded and retain their values throughout
the class's life cycle.

Use Cases and Examples of Static Variables in Java:

The static keyword in Java denotes that a member belongs to the class itself rather than to instances
of the class, enabling shared access among all class instances. Static members can be accessed
before any class instance is created and are instantiated only once. For instance, methods like
Math.abs() and constants like Math.PI in the Math class are static, allowing direct access using the
class name. This guide explores the usage of the static keyword with variables, methods, blocks, and
nested classes through practical examples, providing a comprehensive understanding of its
applications in Java programming.

public class Counter {

private static int count = 0;

public Counter() {

count++;

public static int getCount() {

return count;

In the example, count represents a static variable that maintains track of the amount of Counter
objects were generated. The static function getCount() provides reference to this public variable
without requiring an object.

Advantages:

 Memory Efficiency: Static members are stored in a shared memory location, reducing
memory overhead.
 Accessible Globally: Static methods and variables can be accessed directly using the class
name, simplifying usage.
 Suitable for Utility Functions: Useful for functions that don't rely on instance-specific data.

Limitations:
 Limited Flexibility: Static methods cannot be overridden in subclasses, making them less
flexible for polymorphism.
 Tighter Coupling: Heavy use of static members can lead to tighter coupling between classes,
reducing modularity.

Non-Static Variables in Java

Non-static variables in Java, also referred to as instance variables, are defined within a class without
the static keyword. Unlike static variables, non-static variables are associated with individual
instances (objects) of a class and are stored in heap memory. This means that each object maintains
its own copy of non-static variables, allowing them to hold unique data specific to each instance.
Non-static variables represent object-level data and can be accessed and modified using object
references. They are created when an object is instantiated and are discarded when the object is
garbage collected. The values of non-static variables can be altered throughout the object's lifecycle.

Use Cases and Examples of Non-Static Variables in Java:

Non-static variables in Java, known as instance variables, are specific to each object of a class, with
every object maintaining its own copy of these variables in heap memory. They are essential for
encapsulating object-specific data, representing unique characteristics or attributes for each
instance. Non-static variables are accessed and modified using object references, allowing individual
objects to manage their state independently throughout their lifecycle. This encapsulation ensures
that each object's data remains distinct and isolated from other instances of the same class.

public class Car {

private String model;

public Car(String model) {

this.model = model;

public void drive() {

System.out.println(model + " is driving...");

Within the Car class, model represents an instance variable, while drive() serves as a non-static
function that uses the corresponding variable for carrying out an action unique to each Car instance.

Advantages:

 Supports Polymorphism: Non-static methods can be overridden in subclasses, allowing for


method specialization.
 Encapsulation: Instance variables can maintain encapsulation, keeping data private to each
object.

Limitations:

 Increased Memory Usage: Each instance holds its copy of instance variables, potentially
consuming more memory.
 Requires Object Instantiation: Non-static methods and variables can only be accessed
through object instances, which can be cumbersome in some scenarios.

References:

Chhonkar, V. (2023, March 2). Difference between Static and Non-Static Variables in Java.
PrebBytes.com. https://www.prepbytes.com/blog/java/difference-between-static-and-non-static-
variables-in-java/

Ravikiran, A. S. (2024, April 11). Static in Java: An Overview of Static Keyword With Examples.
Simplilearn.com. https://www.simplilearn.com/tutorials/java-tutorial/static-keyword-in-java

Metta, N. (2024, January 7). Demystifying Java: Static vs. Non-Static Variables, Methods, and Classes.
Medium.com. https://naveen-metta.medium.com/demystifying-java-static-vs-non-static-variables-
methods-and-classes-f74f94ee7527

You might also like