Computer Applications
What is meant by a static data member? Show its application with the help of an example
Answer
The data members of a class that are declared using the keyword static are called static data members. All objects of a class share the same copy of static data members. They are usually accessed using the class name instead of an object. Below example shows the use of static data member count to keep the track of the total number of objects created for the class:
class StaticExample {
static int count;
private String name;
public StaticExample(String n) {
name = n;
count++;
}
public void showName() {
System.out.println(name);
}
}
class StaticExampleTest {
public static void main(String[] args) {
StaticExample obj1 = new StaticExample("Red");
StaticExample obj2 = new StaticExample("Blue");
StaticExample obj3 = new StaticExample("Green");
System.out.println("Total objects of StaticExample class = " + StaticExample.count);
}
}
Output of this program is:
Total objects of StaticExample class = 3
Related Questions
Write a class program with the following specifications:
Class name β Matrix
Data members β int array m[][] with 3 rows and 3 columns
Member functions:
- void getdata() β to accept the numbers in the array
- void rowsum() β to find and print the sum of the numbers of each row
- void colsum() β to find and print the sum of numbers of each column
Use a main function to create an object and call member methods of the class.
Write a program to use a class Account with the following specifications:
Class name β Account
Data members β int acno, float balance
Member Methods:
- Account (int a, int b) β to initialize acno = a, balance = b
- void withdraw(int w) β to maintain the balance with withdrawal (balance - w)
- void deposit(int d) β to maintain the balance with the deposit (balance + d)
Use another class Calculate which inherits from class Account with the following specifications:
Data members β int r,t ; float si,amt;
Member Methods:
- void accept(int x, int y) β to initialize r=x,t=y,amt=0
- void compute() β to find simple interest and amount
si = (balance * r * t) / 100;
a = a + si; - void display() β to print account number, balance, interest and amount
main() function need not to be used
Write a program by using class with the following specifications:
Class name β Sale
Data members/ Instance variables:
- String title, author,publication
- double price
Member methods:
- void input() β to accept title, author name and publication name and price of a book
- void display() β to display title, author name and publication name and price of a book
Now, create another class 'Purchase' that inherits class 'Sale' having the following specifications:
Class name β Purchase
Data members/ Instance variables:
- int noc
- int amount;
Member methods:
- void accept() β to enter the number of copies purchased
- void calculate( ) β to find the amount by multiplying number of copies ordered and price (i.e., noc * price)
- void show() β to display the elements describes in base class along with the number of copies purchased and amount to be paid to the shopkeeper
Write a program to define class with the following specifications:
Class name β Number
Data members/ Instance variables:
- int n β to hold an integer number
Member methods:
- void input() β to accept an integer number in n
- void display() β to display the integer number input in n
Now, inherit class Number to another class Check that is defined with the following specifications:
Class name β Check
Data members/ Instance variables:
- int fact
- int revnum
Member methods:
- void find() β to find and print factorial of the number used in base class
- void palindrome() β to check and print whether the number used in base class is a palindrome number or not
[A number is said to be palindrome if it appears the same after reversing its digits. e.g., 414, 333, 515, etc.]