Question about sample Java application

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
 
R

Roedy Green

RuntimeExample apiClient = new RuntimeExample(); ====> [3]
// Initialize connection
apiClient.connect();

if you only call one method you can write that as:

new RuntimeExample().connect();

But then you don't have the apiClient variable to use to call more
methods.

In this case he may have only one call, but perhaps later there will
be more, so he leaves it ready to go. Debugging is easier with more
intermediate variales to examine.


--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
M

Monique Y. Mudama

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?

Are you asking why they don't just call

RuntimeExample.connect()

? If that's the question you're asking, that only works if the method
is declared as static. If the method isn't static, you need an
instance of the class (need to instantiate it, thereby creating an
object) in order to use the method.
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 :)...)?

To the first, yes, and to the second, no. main() is not called by the
constructor unless for some reason you did so explicitly. MyClass.main() is
called implicitly if you run `java MyClass` from the command line, but
note that this doesn't necessarily mean that MyClass is ever
instantiated; main() is static.

I think I also had this confusion/concern when I started dealing with
Java. main() and construction are two separate beasts. Construction
doesn't imply that main() is called, and main() doesn't necessarily
imply construction. In fact, a large number of classes don't have a
main() method at all; they're intended exclusively to be used by other
classes.

Once you get your brain around that, it becomes pretty easy to do
simple unit testing on the libraries you create in Java. You just add
a main() method in which you run your test cases. main() isn't used
in the normal execution of the code, but you can run `java MyLibrary`
and see if your tests pass.
Thanks in advance for any help with understanding this!

Jim

I hope I've helped and not confused.
 
L

Lee Weiner

public static void main(String[] args)
{
RuntimeExample apiClient = new RuntimeExample(); ====> [3]
// Initialize connection
apiClient.connect(); ====> [4]

..<snip>..
}

}
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()"?

Even though the method main() is "inside" the RuntimeExample class, because it
is declared static, it does not have direct access to the methods and
properties of the object. A static method has to instantiate an object and
access the methods through the object reference.
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 :)...)?

The only thing that executes when an object is created is a constructor, not
main() or any other method unless called by code in the constructor.

Lee Weiner
lee AT leeweiner DOT org
 
O

ohaya

The only thing that executes when an object is created is a constructor, not
main() or any other method unless called by code in the constructor.


Lee, Roedy, and Monique,

Thanks, for the replies. I think that I understand the thing about
"main()" now, and I think that I already somewhat understood that they
had to call the methods inside the class using the
"apiClient.<methodname>", but I'm still unclear about the last part of
my first question "the "WHY?" part". I think that I may have been a
little imprecise about what I was asking exactly.

What I was trying to ask was why not do the following (in the following,
I've added other methods inside the class, as there are in the sample),
instead of the original code:

public class RuntimeExample
{
// ** DELETE** private Runtime runtime= null; =====> [1]

private static void connect() =====> [2]
{
..<snip>..
}

private static void method1()
{
..<snip>..
}
..
..
<snip>
..
..
public static void main(String[] args)
{
// ** DELETE ** RuntimeExample apiClient = new
RuntimeExample(); ====> [3]
// Initialize connection
connect(); ====> [4]
method1();

..<snip>..
}

}


It seems like to do as above, I have to make all of the methods inside
the class static (wouldn't compile otherwise :)), so I guess that what I
might really be puzzled about is WHY one would WANT vs. NOT WANT to have
the methods inside the class be static?

Thanks again,
Jim
 
M

Monique Y. Mudama

It seems like to do as above, I have to make all of the methods inside
the class static (wouldn't compile otherwise :)), so I guess that what I
might really be puzzled about is WHY one would WANT vs. NOT WANT to have
the methods inside the class be static?

Thanks again,
Jim

My gut response was, "because then you can't access the non-static
members!", but of course that's not a problem if everything's static.

Here's what is. Static fields have the same value for all instances
of a class. The only class variables a static method can access are
static fields. So if you declare everything as static, everything in
your app that deals with RunTimeExample will always get a
RunTimeExample populated with the same data. That's sometimes, but
not usually, what you want. (Does static actually mean it's shared
across the entire JVM? I don't recall.)

My first couple years of Java programming, I would try to solve compiler
problems as you did here, by peppering the word 'static' around the code
without really understanding what it did. I wasted a lot of time that
way, because once you change something to static, you inevitably find
that other stuff needs to be static to support it, and so on, and
eventually one of the things that needs to be static is something that
really shouldn't be (ie, should have different values across classes).
 
C

Chris Head

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ohaya wrote:
[snip]
It seems like to do as above, I have to make all of the methods inside
the class static (wouldn't compile otherwise :)), so I guess that what I
might really be puzzled about is WHY one would WANT vs. NOT WANT to have
the methods inside the class be static?

Thanks again,
Jim


Hi,
Making all the methods in your class static essentially destroys the
advantages that object-oriented programming gives you. Consider the
following class:

public class Person {
private static String name;
private static int height;

public static void hello() {
System.out.println("Hi, I am " + name + ", " + height + "ft tall.");
}

public static void main(String[] args) {
name = "Joe";
height = 6;
hello();
}
}

The problem is that you can only describe one person at a time. By
making things non-static, you can do this:

public class Person {
private final String name;
private final int height;

public Person(String name, int height) {
this.name = name;
this.height = height;
}

public void hello() {
System.out.println("Hi, I am " + name + ", " + height + "ft tall.");
}

public static void main(String[] args) {
Person joe = new Person("Joe", 6);
Person shorty = new Person("Shorty", 5);
joe.hello();
shorty.hello();
}
}

As seen above, I have now created two separate people simultaneously and
stored them in different variables. That is the purpose of non-static
methods and variables.

Please excuse syntax errors; I have not compiled these examples.

Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32)

iD8DBQFC7RE56ZGQ8LKA8nwRAsxRAJ0WhqqYyHV5rWnCYnWHz0MKtb4TuwCffiiY
v4nJ3jv59Yv3cpFNUI+hiFk=
=X/d1
-----END PGP SIGNATURE-----
 
O

ohaya

Hi,
Making all the methods in your class static essentially destroys the
advantages that object-oriented programming gives you. Consider the
following class:

public class Person {
private static String name;
private static int height;

public static void hello() {
System.out.println("Hi, I am " + name + ", " + height + "ft tall.");
}

public static void main(String[] args) {
name = "Joe";
height = 6;
hello();
}
}

The problem is that you can only describe one person at a time. By
making things non-static, you can do this:

public class Person {
private final String name;
private final int height;

public Person(String name, int height) {
this.name = name;
this.height = height;
}

public void hello() {
System.out.println("Hi, I am " + name + ", " + height + "ft tall.");
}

public static void main(String[] args) {
Person joe = new Person("Joe", 6);
Person shorty = new Person("Shorty", 5);
joe.hello();
shorty.hello();
}
}

As seen above, I have now created two separate people simultaneously and
stored them in different variables. That is the purpose of non-static
methods and variables.

Please excuse syntax errors; I have not compiled these examples.

Chris


Chris and Monique,

Thanks for the responses again, and (esp.) Chris, thanks for the
example. I think I understand some things now. Let me try to
explain...

In most of what I've been doing (my job), when I have to do a Java
application, they tend to be "one shot" things, mainly a utility or tool
for doing something specific, so I typically will just write a
'standalone' application, and that will typically be in just one
standalone .java file, with a main() and all the methods for the
application within that one .java file. I've rarely had to write a Java
application that needs a bunch of different classes that are just used
by the application.

Because of that, it's just been a lot easier (e.g., for CM, etc.) to put
everything into one Java class, making the methods in the class
'static', because I just have to source control the one .java file.

Then I ran across the sample application that I mentioned, and it had
that structure where it did a "new" in the main(), and, given the
application structure that I was "use to", I couldn't understand why
whoever had written the sample had gone that way.

Now, I think I do.

It looks like they intended that sample to be used both as a standalone
application (and thus it has a main() method) and, potentially, for it
(the sample) to be used as a class that could be instantiated by someone
else who was writing a Java application.

I think the other thing that kind of threw me was that in the sample
code/class, they made ALL of the methods, except for the main() method,
private. This confused me because if they intended this class to be
instantiated by another Java application, there would be no way to
access any of the methods within this RuntimeExample class. That still
doesn't make much sense to me, but maybe that's just the way whoever
wrote this sample code was use to writing...


The above is a kind of "stream of consciousness" explanation of how I've
been thinking about all of this, and I hope that it makes sense to you
all.

Thanks for all of your help. This has been very interesting (and
educational) for me!

Jim
 
M

Monique Y. Mudama

In most of what I've been doing (my job), when I have to do a Java
application, they tend to be "one shot" things, mainly a utility or
tool for doing something specific, so I typically will just write a
'standalone' application, and that will typically be in just one
standalone .java file, with a main() and all the methods for the
application within that one .java file. I've rarely had to write a
Java application that needs a bunch of different classes that are
just used by the application.

Because of that, it's just been a lot easier (e.g., for CM, etc.) to
put everything into one Java class, making the methods in the class
'static', because I just have to source control the one .java file.

One thought: as you gain more familiarity with Java, you will probably
find that what "didn't need a bunch of different classes" actually would
be much easier to write if it were designed that way. And if you find
yourself copy/pasting any of the code, you may want to look into forming
your own library of utilities.

I don't suppose there's a software engineer with a great deal of
experience in Java and OO design in your work environment? I was
fortunate enough to have that experience starting out in the language,
and it was a huge help.
 
B

blmblm

In most of what I've been doing (my job), when I have to do a Java
application, they tend to be "one shot" things, mainly a utility or tool
for doing something specific, so I typically will just write a
'standalone' application, and that will typically be in just one
standalone .java file, with a main() and all the methods for the
application within that one .java file. I've rarely had to write a Java
application that needs a bunch of different classes that are just used
by the application.

Because of that, it's just been a lot easier (e.g., for CM, etc.) to put
everything into one Java class, making the methods in the class
'static', because I just have to source control the one .java file.[/QUOTE]

There's an old saying about how a determined Fortran programmer
can write Fortran programs in any language. I think you're doing
something similar here. :) It's kind of an abuse of Java, in the
sense of being highly unidiomatic, but if you don't have time to study
the language in detail and just want to get something running ....
Then I ran across the sample application that I mentioned, and it had
that structure where it did a "new" in the main(), and, given the
application structure that I was "use to", I couldn't understand why
whoever had written the sample had gone that way.

Now, I think I do.

It looks like they intended that sample to be used both as a standalone
application (and thus it has a main() method) and, potentially, for it
(the sample) to be used as a class that could be instantiated by someone
else who was writing a Java application.

Or it's possible that the sample is written that way because that's
how most Java classes are written, which makes it possibly better
style, or at least more idiomatic.
I think the other thing that kind of threw me was that in the sample
code/class, they made ALL of the methods, except for the main() method,
private. This confused me because if they intended this class to be
instantiated by another Java application, there would be no way to
access any of the methods within this RuntimeExample class. That still
doesn't make much sense to me, but maybe that's just the way whoever
wrote this sample code was use to writing...

I think that's it -- it's what people who write a lot of Java are
likely to be used to.

[ snip ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,079
Latest member
ElidaWarin

Latest Threads

Top