
Patrick B. answered 03/23/21
Math and computer tutor/teacher
copy and paste the code into the text editor...
for #1, save as Demo.java and #2 as ConsDemo.java;
compile ..
run...
(1)
Inside Constructor
Value1 === 10
Value2 === 20
(2)
10 20
Lyncy M.
asked 03/23/211. PROGRAM01.
class Demo{
int value1;
int value2;
Demo(){
value1 = 10;
value2 = 20;
System.out.println("Inside Constructor");
}
public void display(){
System.out.println("Value1 === "+value1);
System.out.println("Value2 === "+value2);
}
public static void main(String args[]){
Demo d1 = new Demo();
d1.display();
}
}
2. PROGRAM02.
// A simple constructor.
class MyClass {
int x;
// Following is the constructor
MyClass(int i ) {
x = i;
}
}
public class ConsDemo {
public static void main(String args[]) {
MyClass t1 = new MyClass( 10 );
MyClass t2 = new MyClass( 20 );
System.out.println(t1.x + " " + t2.x);
}
}
Patrick B. answered 03/23/21
Math and computer tutor/teacher
copy and paste the code into the text editor...
for #1, save as Demo.java and #2 as ConsDemo.java;
compile ..
run...
(1)
Inside Constructor
Value1 === 10
Value2 === 20
(2)
10 20
Get a free answer to a quick problem.
Most questions answered within 4 hours.
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.