File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JavaFX and the fly likes Please someone explain me the following code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JavaFX
Bookmark "Please someone explain me the following code" Watch "Please someone explain me the following code" New topic
Author

Please someone explain me the following code

Pallab Pal
Greenhorn

Joined: Dec 22, 2013
Posts: 8
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

Joined: Jan 26, 2012
Posts: 130
    
    9
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

Joined: Dec 22, 2013
Posts: 8
Thank you John
But the example is a copy of netbeans team "ensamble2"
John Damien Smith
Ranch Hand

Joined: Jan 26, 2012
Posts: 130
    
    9
> 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

Joined: Dec 22, 2013
Posts: 8
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

Joined: Jan 26, 2012
Posts: 130
    
    9
> 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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please someone explain me the following code
 
Similar Threads
compiling a non packaged class
Difference between Class.forName("ClassName") and ClassName.class
why main method
static methods
Doubt regarding invoking a method using super keyword