Re: Question about sample Java application




>
> 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
.



Relevant Pages

  • Re: beginners question
    ... It must accept a String array as a parameter. ... public static void main ... When java tells you that it cannot find a method, ... requiring an applet viewer. ...
    (comp.lang.java)
  • JNI - unresolved _ZNSs4_Rep11_S_terminalE
    ... which (the main-driver) calls JAVA again to ... do the real work for processing the message. ... when starting the JAVA MainWrapper ... public static void main{ ...
    (comp.lang.java.programmer)
  • wrong ELF class: ELFCLASS64 when trying to link dynamic .so library from Java/JNA
    ... I have "installed" JNA in order to be able to access native calls from ... thus I would like a Java interface). ... public static void main(Stringargs) { ...
    (comp.unix.programmer)
  • Re: beginners question
    ... It must accept a String array as a parameter. ... public static void main ... When java tells you that it cannot find a method, ... requiring an applet viewer. ...
    (comp.lang.java)
  • Re: Inner Classes a liability
    ... "Are private members inherited ?" ... Sun Certified Developer for the Java 2 Platform ... >> defining what it was exactly that inner classes were supposed to do. ... public static void main ...
    (comp.lang.java.programmer)