• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please someone explain me the following code

 
Pallab Pal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Sample extends Application {
private static Sample sample1;

public static Sample getSample() {

return sample1;
}
@Override public void start(final Stage stage) {

sample1 = this;
/*more statements here*/
stage.setScene(scene);
stage.show();
}
public static void main(String[ ] args) {
Application. launch(args);
}
}

Can anyone tell me how getSample function is called in the above example and why it is used and also why sample1 = this; is used?
 
John Damien Smith
Ranch Hand
Posts: 299
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is a kind of weak implementation of the Singleton pattern for the Sample Application.

It is not really a singleton (as it does not enforce that there is only one version of Sample created), but generally with the JavaFX runtime you only launch a single application in the java virtual machine, so it's reasonably safe.

The purpose of creating the static sample1 reference then providing a static method to getSample(), is that the Sample instance then becomes effectively global in that you can always get a reference to the application by calling Sample.getSample(), so any code your could get the sample application parameters by calling Sample.getSample().getParameters()

Static methods and members in general are described in:
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

You may also be interested in:
http://code.google.com/p/google-singleton-detector/wiki/WhySingletonsAreControversial
http://stackoverflow.com/questions/2671496/java-when-to-use-static-methods
 
Pallab Pal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you John
But the example is a copy of netbeans team "ensamble2"
 
John Damien Smith
Ranch Hand
Posts: 299
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> But the example is a copy of netbeans team "ensamble2"

The Ensemble sample application comes from the JavaFX development team, not the NetBeans team.
The code in your question is pretty generic and standalone, it doesn't really matter if it came from Ensemble or not.
Did my answer explain the code for you or do you still have difficulties understanding it?
 
Pallab Pal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Still I don't understand how getSample function is
called in the above example and why it is used
and also why sample1 = this; is used?
 
John Damien Smith
Ranch Hand
Posts: 299
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I don't understand how getSample function is called in the above example

getSample is never called in the above example. If you wanted to call it though, you could call it using:


> why it is used

It allows any code in the system to get an instance of the Sample application object so that it can invoke functions on it, for example:


> sample1 = this; is used?

In order for getSample(), to work the sample1 variable needs to be assigned a Sample instance, thath is what the sample1 = this code does.

----

I'm afraid the above answers don't explain any better than my previous answer (they are largely the same), but I don't know of another way to explain this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic