• 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

constructor question

 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q8)Which of the following are valid Constructors?

A1 public Test8(){}
A2 private void Test8(){}
A3 protected Test8(int k){}
A4 Test8(){}

from

http://www.javabeat.net/javabeat/scjp2/mocks/

The answer being given is

A1,A3
what is wrong with "A4" option
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing seems wrong to me.. Might be an error in the mock test.

contact the site and see what they say..
[ April 02, 2008: Message edited by: Justin Russo ]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A4 is a valid constructor too. must have been missed out
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for replies.
now i am confident.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes the A4 option is the Valid constructor.
--
Ishmayel
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test14{
static String s ="Instance";
public static void method(String s){
s+="Add";
}
public static void main(String a[]){
Test14 t = new Test14();
s = "New Instance";
String s = "Local";
method(s);
System.out.println(s);
System.out.println(t.s);
}
}
What is output?

A1 Local Instance
A2 Local New Instance
A3 Loca Add New Instance
A4 Compiler Error

from
http://www.javabeat.net/javabeat/scjp2/mocks/scjp_1_4_mock_exam_questions_2.php

What's your take on this question?
I say compiler error because of duplicate variable.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sandeep Bhandari:
public class Test14{
static String s ="Instance";
public static void method(String s){
s+="Add";
}
public static void main(String a[]){
Test14 t = new Test14();
s = "New Instance";
String s = "Local";
method(s);
System.out.println(s);
System.out.println(t.s);
}
}
What is output?

A1 Local Instance
A2 Local New Instance
A3 Loca Add New Instance
A4 Compiler Error

from
http://www.javabeat.net/javabeat/scjp2/mocks/scjp_1_4_mock_exam_questions_2.php

What's your take on this question?
I say compiler error because of duplicate variable.




This program just by simply compiling and running gives



Local
New Instance



I assumed that it will give a compiler error because of the "forward reference" issue

s = "New Instance"
String s = "Local";

first s is initialized and then it is declared.

Please correct me if I am wrong.

how come it is printing

Local
New Instance

Thanks
 
Ranch Hand
Posts: 376
Scala Monad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the beginning of the class:
static String s ="Instance";

When you do
s = "New Instance"
you're assigning the value "New Instance" to the class variable s

When you do
String s = "Local";
You're declaring a new local String s that "eclipses" the class variable.
The original s variable is still accessible as t.s or better, as Test14.s (since is a static/class variable)
 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gabriel Claramunt:
Look at the beginning of the class:
static String s ="Instance";

When you do
s = "New Instance"
you're assigning the value "New Instance" to the class variable s

When you do
String s = "Local";
You're declaring a new local String s that "eclipses" the class variable.
The original s variable is still accessible as t.s or better, as Test14.s (since is a static/class variable)



Thanks a lot for your reply. It surely clears up the concept.
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test14{
static String s ="Instance";
public static void method(String s)
{
s+="Add";
System.out.println(s);
}
public static void main(String a[])
{
Test14 t = new Test14();
s = "New Instance";
//String s = "Local"; -- line 1
method(s);
System.out.println(s);
System.out.println(t.s);
}
}

o/p --
New InstanceAdd
New Instance
New Instance

When i comment the line 1 , i get the above o/p.
****************************************************

public class Test14{
static String s ="Instance";
public static void method(String s)
{
s+="Add";
System.out.println(s);
}
public static void main(String a[])
{
Test14 t = new Test14();
//s = "New Instance"; -- line 2
String s = "Local";
method(s);
System.out.println(s);
System.out.println(t.s);
}
}

o/p --
LocalAdd
Local
Instance

when comment line 2, i get above o/p

I didn't get. String s is static variable so it has one copy to all instances of class.
s="New Instance"
String s="Local"

What these are doing , can you please hlep me out ot understand the concept please...
[ April 02, 2008: Message edited by: Dinesh Tahiliani ]
reply
    Bookmark Topic Watch Topic
  • New Topic