• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

String operation

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Please explain,

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
The ouptut is A2.
Thankyou
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is testing your knowledge of variable scope.



You have three String variables with the same name and different scopes in this example. You have the static instance variable at mark 1. You have a String reference at mark 2 being passed in as a parameter to the method. This reference is only in scope during the run of that method body. Finally, you have a local reference in the main method at the line marked 4.

When the main method is run, you create a new object of type Test14. Its instance field is given the value "Instance". Then the local variable is declared in main and initialized with the value "New Instance"; That reference is passed into the method. Remember that in Java references are pass by copy (a copy of the reference is passed into the method, not a copy of the data). So, inside the method, you can read the data being referenced by the input parameter, but any changes are not written to the reference that you passed in. So, inside the method the reference is modified, but once the method returns the change is lost. Then you print the output.

Hope this helps a little.
 
Ranch Hand
Posts: 37
IntelliJ IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is:
Local
New Instance
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think whatever Bill had replied, would suffice. Please read his reply patiently and if required one more time.

Adding to that, going through this thread may help you to understand the concepts clear. {If you dont understand the basic of *pass by value Vs pass by reference*}.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A small change in Bill Cruise's input.






When the main method is run, you create a new object of type Test14. Its instance field is given the value "Instance".



The class level (static) field is initialized with the value "Instance" (because of "static") and its NOT an instance field!


Then the local variable is declared in main and initialized with the value "New Instance"; That reference is passed into the method.



The same static (class-level) variable is assigned with a new value and NOT the local variable is.

Actually, the local (String reference) variable here is s and it is initialized with the literal "Local" as in line 4.

Hope this helps.

[ June 11, 2007: Message edited by: Raghavan Muthu ]
[ June 11, 2007: Message edited by: Raghavan Muthu ]
 
Don't play dumb with me! But you can try this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic