I am trying to understand J2ME code
Thread aaa = new Thread(pb) { private final ProgressBar val$pb;
public void run() { while (this.val$pb.getValue() < 100) {
try {
this.val$pb.setValue(this.val$pb.getValue() + 1);
Thread.sleep(10L); } catch (InterruptedException ex) {
}
this.val$pb.repaint();
}
mainForm.homeScreen = new HomeScreen();
mainForm.homeScreen.show();
}
};
aaa.start();
please tell me what does pb
do in Thread
constructor. How this code will look like if I Change new Thread(pb)
to new Thread()
? Does it affect val$pb
? above code could not be compiled so I edited like this
Thread aaa = new Thread() { private final ProgressBar val=null ;//new Thread(pb) ProgressBar val$pb;
public void run() { try {while (this.val.getValue() < 100) { //try added by me
try {
this.val.setValue(this.val.getValue() + 1);
Thread.sleep(10L); } catch (InterruptedException ex) {
}
this.val.repaint(); //draws progress bar as a loading screen before showing home screen
}
}catch(Exception e){
mainForm.homeScreen = new HomeScreen();
mainForm.homeScreen.show(); // draws home screen
}
}
};
aaa.start();
EDIT:- it uses J4ME library.
If your code compiles then pb
is a parameter to Thread
constructor.
Given that it is a single parameter, it may be either Runnable or String since Java ME API specifies only these objects possible as a single parameter in Thread constructor.
How this code will look like if I Change new Thread(pb) to new Thread()?
If pb is Runnable
then things likely won't change because Thread instance aaa
overrides run method (which would be otherwise invoked for pb) and because in your code snippet, there are no traces of invoking pb.run
anyway else (which smells of design error or of too much code cut from your snippet).
If pb is String
then name of the Thread aaa
will be default instead of pb value.
Does it affect val$pb?
Hard to tell unless you post more code - preferably in SSCCE form.
val$pb
looks funny but this could be a legal identifier for a variable assuming that in the code snippet you have cut something that initializes it.
update in the second version of your code, given that you initialized private final ProgressBar val=null
- as a result, in the run method statements that invoke methods on it will throw NPE this.val.getValue()
and proceed right into catch block that draws home screen according to code comments.
update2
if I don't initialize
private final ProgressBar val
then it gives error asvariable val might not have been initialized
so what would be another way to initialize it?
Well with limited amount of code in your snippet one can only guess.
One way that comes to mind is to init with pb
, like this:
//...
new Thread(pb) { private final ProgressBar val$pb = pb; // ...
above may compile if pb refers to ProgressBar instance and is declared final.
Note that in this case, val$pb
is not quite necessary because pb
can be used instead of it (maybe this variable was introduced for code style preferences).
Also, given your reference to j4me have to admit that using ProgressBar in thread constructor - if pb is an instance of this class - makes very little sense to me. One can only wonder how did it appear there in the initial snippet you posted Thread aaa = new Thread(pb)...