Menu
Topics Index
...
`

Static method and variables1494


If you need explanation Read this topic

If you need Answer Take test on this topic

Static method and variables1494
What will be the output of the following program?
public class Student {
    int rollno;
    String name;
    static String college = "RITA";
    static void chage() {
        college = "SRIT";
    }
    Student(int r, String n) {
        rollno = r;
        name = n;
    }
    void display() {
        System.out.println(rollno + " " + name + " " + college);
    }
    public static void main(String arts[]) {
        Student.chage();
        Student s1 = new Student(516, "Kiran");
        Student s2 = new Student(560, "Vishwanath");
        Student s3 = new Student(517, "Kranthi");
        s1.display();
        s3.display();
        s2.display();
    }
}

A. 516 Kiran SRIT
516 Kiran SRIT
516 Kiran SRIT
B. 516 Kiran SRIT
517 Kranthi SRIT
560 Vishwanath SRIT
C. 516 Kiran RITA
516 Kiran RITA
516 Kiran RITA
D. 516 Kiran RITA
517 Kranthi RITA
560 Vishwanath RITA
E. Compilation Error or Runtime Error
Topic: Static Keyword In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is B.
Here we need to know about the scope of static and instance variables...

Student class contains 2 instance variables rollno and name along with a static variable college which shares same data for all the objects...

Before creating objects we are changing the college name from RITA to SRIT by calling static method change() by Student class.. static methods are accessed directly by class name..
Now we are creating 3 objects of Student class with different rollno and name for each Student object.. Then we are displaying the values by calling the display() method on particular object..

On s1 object it prints 516 as rollno and Kiran as name and SRIT as college.. On s3 object it prints 517 as rollno and Kranthi as name and SRIT as college.. On s2 object it prints 560 as rollno and Viswanath as name and SRIT as college..

Thus output is option B.

Posted by Gopinath Manchikanti    2015-03-02 13:38:42


ANS IS B

--Here,student class is defined with the attributes name,roll no and college name.

Following function() ate defined:
1.change()
2.display()

One constructor with 2 parameters roll no and name is defined.

--After that, change method which is static is called that changes the name of college "RITA" to "SRIT".

--Following three objects of student class are created by passing 2 parameter I.e. s1,s2 &s3.After that display method of  student class is called by using the object.

OUTPUT:
516 kiran SRIT
517 kranthi SRIT
560 vishwanath SRIT

Posted by Mânïshå Mùlchåndânï    2015-03-02 17:44:00


ans b. static is used to define global variable. The Java programming language supports static methods as well as static variables. Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in ClassName.methodName(args). coming to this program we called static method change. in that method we assigned college to SRIT.then we created 3 objects with 2 passing arguments.roll number and name passed.  it invokes constuctor and stores these passed values using instance variable. by using object reference display method called and sop prints corresponding values. s1.display() prints "516 kiran SRIT",s3.display prints "517 kranthi SRIT ",s2.display() prints " 560 vishwanath SRIT"

Posted by Maheshwari Natarajan    2015-03-02 18:38:14


This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Mânïshå Mùlchåndânï, Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Cool Pawan. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-03-03 03:50:32


© meritcampus 2019

All Rights Reserved.

Open In App