• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reference to objects

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Badri Sarma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Lawrence Chettiar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic