O
ohaya
Hi,
I'm working with some sample code out from a vendor's SDK, and they have
a small sample application called RuntimeExample.java. The application
has the following structure:
public class RuntimeExample
{
private Runtime runtime= null; =====> [1]
private void connect() =====> [2]
{
..<snip>..
}
..
..
<snip>
..
..
public static void main(String[] args)
{
RuntimeExample apiClient = new RuntimeExample(); ====> [3]
// Initialize connection
apiClient.connect(); ====> [4]
..<snip>..
}
}
This is probably pretty simple stuff to most of you here, but I'm still
kind of new to working with Java applications, and I want to try to
understand how this code is working, so I have some questions, and hope
that I might get some help here:
1) On lines [3] and [4], I think that line [3] is instantiating a new
instance of the RuntimeExample class, then in line [4], they are calling
the connect() method in the new instance of the RuntimeExample class,
but I don't understand 'WHY' they are doing this, rather than not having
the "RuntimeExample apiClient = new..." line and just calling
"connect()" instead of "apiClient.connect()"?
Also, what are the advantanges of doing things this way?
2) When the new instance of RuntimeExample is created on line [3], does
this just create the new instance, and is the 'main(...)' method in the
new instance not automatically invoked when the new instance is created
(I have pictures of infinite recursion in my mind ...)?
Thanks in advance for any help with understanding this!
Jim
I'm working with some sample code out from a vendor's SDK, and they have
a small sample application called RuntimeExample.java. The application
has the following structure:
public class RuntimeExample
{
private Runtime runtime= null; =====> [1]
private void connect() =====> [2]
{
..<snip>..
}
..
..
<snip>
..
..
public static void main(String[] args)
{
RuntimeExample apiClient = new RuntimeExample(); ====> [3]
// Initialize connection
apiClient.connect(); ====> [4]
..<snip>..
}
}
This is probably pretty simple stuff to most of you here, but I'm still
kind of new to working with Java applications, and I want to try to
understand how this code is working, so I have some questions, and hope
that I might get some help here:
1) On lines [3] and [4], I think that line [3] is instantiating a new
instance of the RuntimeExample class, then in line [4], they are calling
the connect() method in the new instance of the RuntimeExample class,
but I don't understand 'WHY' they are doing this, rather than not having
the "RuntimeExample apiClient = new..." line and just calling
"connect()" instead of "apiClient.connect()"?
Also, what are the advantanges of doing things this way?
2) When the new instance of RuntimeExample is created on line [3], does
this just create the new instance, and is the 'main(...)' method in the
new instance not automatically invoked when the new instance is created
(I have pictures of infinite recursion in my mind ...)?
Thanks in advance for any help with understanding this!
Jim