aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static string" Watch "static string" New topic
Author

static string

Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
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?


Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
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 ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Rahul Bhosale
Ranch Hand

Joined: Mar 10, 2005
Posts: 77
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.


RB
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
---------------------------------------------------------------------
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

Joined: Mar 17, 2005
Posts: 33
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

Joined: Mar 02, 2005
Posts: 580
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

Joined: Mar 02, 2005
Posts: 580
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

Joined: Mar 01, 2005
Posts: 485
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

Joined: Mar 02, 2005
Posts: 580
---------------------------

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

Joined: Mar 01, 2005
Posts: 485
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

Joined: Mar 17, 2005
Posts: 33
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
 
 
subject: static string