JAVA lab 6
JAVA lab 6
Am.sc.u4aie24029
int roll;
String name;
String address;
Student() {
count++;
this();
this.roll = r;
this.name = n;
this.address = a;
s.roll = 13;
s.name = "durga";
}
III. What did you observe from output of lines 4,5,6?
Explain your answer.
A: 4TH line 0 because the default constructor doesn't set
roll
5th line 12 because roll set through constructor
6th line after13 roll modified by the static method.
A: OUTPUT
static block runs when class is loaded before main
in main direct statement in main()
in cons constructor is called when object is created
3.
A:
Missing constructor argument:
Foo f = new Foo(); // ERROR
The class Foo has only one constructor:
public Foo(int x)
So this line must pass an integer.
For example:
Foo f = new Foo(10); 2
Typo in System.out.println:
System.out.prinln(a);
public class Foo {
private int a;
public Foo(int x) { a = x;
}
public void print() {
System.out.println(a);
}
}
public class FooTest {
public static void main(String[] args){
Foo f = new Foo(10);
f.print();
}
}
5.
A: The method call myMethod(); inside main will cause a
compile-time error.
Non-static method myMethod() cannot be referenced
from a static context
Make myMethod static
6.
A:
OUTPUT
7.
A:
OUTPUT