English

What is meant by scope of variables? Show its application with the help of an example.

Advertisements
Advertisements

Question

What is meant by scope of variables? Show its application with the help of an example. 

Code Writing
Short Answer
Advertisements

Solution

A static member is an instance variable that is global to the class. All class objects share a single copy of this data.

Example:

public class Stud{
static int count; // static variable
int x; // ordinary variable
void display()
{
System.out.println();
System.out.print("Count: " + ++count);
System.out.print(" X = " + ++x);
   }
   public static void main(String args[])
   {
   Stud ob1 = new Stud();
   ob1.display();
   Stud ob2 = new Stud();
   ob2.display();
   Stud ob3 = new Stud();
   ob3.display();
   }
}

Output:

Count:1 x=1
Count:2 x=1
Count:3 x=1

shaalaa.com
  Is there an error in this question or solution?
Chapter 8: Encapsulation and Inheritance - EXERCISES [Page 446]

APPEARS IN

Avichal Computer Applications [English] Class 10 ICSE
Chapter 8 Encapsulation and Inheritance
EXERCISES | Q V. 12. | Page 446
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×