Forums Register Login

Reference to objects

+Pie Number of slices to send: Send
Hi,
What should be the output of the below given code. I think it should be
p: (0,0)
default
a: { (0,0), (1,1) }
hello
class Point {
int x, y;
Point() { System.out.println("default"); }
Point(int x, int y) { this.x = x; this.y = y; }
// A Point instance is explicitly created at class initialization time:
static Point origin = new Point(0,0);
// A String can be implicitly created by a + operator:
public String toString() {
return "(" + x + "," + y + ")";
}
}
class Test {
public static void main(String[] args) {
// A Point is explicitly created using newInstance:
Point p = null;
try {
p = (Point)Class.forName("Point").newInstance();
} catch (Exception e) {
System.out.println(e);
}
// An array is implicitly created by an array constructor:
Point a[] = { new Point(0,0), new Point(1,1) };
// Strings are implicitly created by + operators:
System.out.println("p: " + p);
System.out.println("a: { " + a[0] + ", "
+ a[1] + " }");
// An array is explicitly created by an array creation expression:
String sa[] = new String[2];
sa[0] = "he"; sa[1] = "llo";
System.out.println(sa[0] + sa[1]);
}
}
Tell me if i am wrong
Regards
+Pie Number of slices to send: Send
Hi Badrinath
The output should be
default
p: (0,0)
a: {(0,0),(1,1)}
hello
bcoz, when at line
p = (Point)Class.forName("Point").newInstance();
is executed a instance of point class is made with default constructor hence first output will be default.
Hope it helped .
then the next set of outputs which are obvious.
+Pie Number of slices to send: Send
Hi,
But if the variable or block is declared as static it gets initilized before the instance of class is created (i.e. when class is loaded).
If we see below code
public class Test {
Test(String str){
System.out.println("constructor"+str);
}
public static void main(String[] args){
West t = new West();
}
};
class West
{
static{
Test t1 = new Test("argu");System.out.println("static block");
}
West(){
System.out.println("West Constructor");
}
};
the output is :
constructorargu
static block
West Constructor
question is in the first question which i posted,
why the static object is not getting executed before the creation of instance of the class Point. I fell that output should be in this order for the first question
p: (0,0)
default
a: { (0,0), (1,1) }
hello
am i missing any concept. Pls correct me
Thankingyou
+Pie Number of slices to send: Send
Absolutely correct there is nothing wrong with your concept at all its fully correct
but if you see the code closely the static point origin = new Point(0,0) does execute before
p = (Point)Class.forName("Point").newInstance(); but there is no print statment in the (int,int) constructor hence you can only see the output of the below created array,
If you happen to modify the (int,int) constructor and add a print line then it will print as
(0,0) {comming from static class
default {for newInstance()}
p 0,0) {for System.out.println("p: " + p);}
a: { (0,0), (1,1) } {for array}
hello
Hope this clears things.
I just had the craziest dream. This tiny ad was in it.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 775 times.
Similar Threads
Can someone explain this question to me? please
is rectangle?
Result of the Code
User Defined Exception problem !
output
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 02:49:53.