Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static string

 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Tux1 extends Thread{
static String sName = "vandeleur";
public static void main(String argv[]){
Tux1 t = new Tux1();
t.piggy(sName);
System.out.prinln(sName);
}
public void piggy(String sName){
sName = sName + " wiggy";
start();
}
public void run(){
for(int i=0;i < 4; i++){
sName = sName + " " + i;
}
}
}

Output is
vandeleur

I am confused the above result. Could any one explain about the output?
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes..
Strings are immutable and Since the sName formal parameter in method piggy() is just a copy of the sName field in class Tux, the original sName still refers to the original string "vandeleur".

The parameter in this method piggy hides the class member.
[ April 08, 2005: Message edited by: Srinivasa Raghavan ]
 
Rahul Bhosale
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
put this line of code in the piggy method right after you assign the new string to sName

or change the following line of code
to

I'd love to get a more detailed explanation from the gurus.
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
---------------------------------------------------------------------
Output is
vandeleur

I am confused the above result. Could any one explain about the output?

----------------------------------------------------------------------

While a child thread is started we cannot assure that always child thread execute immediately before main continues its code....

So makes little change.....

public class Tux extends Thread
{
static String sName = "vandeleur";
public static void main(String argv[]) throws InterruptedException
{
Tux t = new Tux();
t.piggy(sName);
System.out.println(sName);
}
public void piggy(String sName) throws InterruptedException
{
int i=10;
sName = sName + " wiggy";
start();
join(); //Added line
}
public void run()
{
for(int i=0;i < 4; i++)
{
sName = sName + " " + i;
}
}
}


and the desire output.............

vandeleur 0 1 2 3

i hope it clers U all
 
Baiju Scariah
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your program main thread (main method) may complete its execution before the child thread start or complete its execution..

Thus the modification you are doing in child thread may not affect the println statement that is given in the main thread.
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Main program ensures the child thread to be in atleast once in a running stage before main thread continues its next instruction

that one slot enough to complete its all works in our program
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry friends

in my last reply i told that child thread have given a one slot before main
gots another slot

but it is not true

When we use join, child thread after terminates only main thread works

Sorry for confusion.....
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi vidyasagar i didn't get the join method
can u explain me?

to my knowledge i understand that the child thread will be started once it invoked the start() method.The line that follows after the start() method will be carried on by main thread.(i.e)The join method will be invoked by main thread.by we didn't give any information to tell the main thread to join the child thread, thats were i am confused can just explain me about join method
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
---------------------------

to my knowledge i understand that the child thread will be started once it invoked the start() method.The line that follows after the start() method will be carried on by main thread.(i.e)The join method will be invoked by main thread.by we didn't give any information to tell the main thread to join the child thread, thats were i am confused can just explain me about join method

---------------------------

Hi Paramesh

Whenever we calling a start method of Thread it makes a newly created Thread to runnable state (not Running state).To Make newly created Thread to be run we call a join() method to make it run, by stopping currently running Thread. Once the Thread started running it will finish all instruction and handover over to the calling thread.

In our program mention Above..... Since we didnot specify any object it takes this Object(Not this Thread be ..Careful) and started to run.

I hope now UR Clear.....
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i doubt in the connection between the join and running the thread.
any how
can u explain with some example if u don't mind
 
Baiju Scariah
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A extends Thread{
// Some code here
main()
{
A a1 = newA();
A a2 = newA();
A a3 = newA();

a1.start();
a1.join();

a2.start();
a2.join();

a3.start();
a3.join();

System.out.println("main");
}
}

In the above example a1, a2 and a3 will be executed sequentially.

If join is called on one object then only after that object completes its execution main will continue its execution

- you know otherwise java will not guarantee about the order of execution
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic