Database Reference
In-Depth Information
I imagine this intended to compare int1 with int2 , but it ended up setting the value of int1 equal to
the value of int2 . Bad, very bad—too bad.
Because String is an object, you need to treat it like an object and call its methods for comparisons.
We call String.equals() method to compare Strings .
if( stringOne.equals( stringTwo ) )
This requirement for using methods of objects to do comparisons applies to all object types.
However, it is most common to run into coding errors in this regard when dealing with Strings : they
probably appear to programmers to be more like the primitives.
Static Modifier and the main() Method
You have seen the modifier static already. It was in our code example in the definition of the main()
method, and in the definition of the myRef member. Both members and methods can be static. To
describe what static means, let me step back for a second.
Imagine you executed java.exe and are running some Java code. Picture the JVM holding a bunch of
objects (instances). If our code created 100 instances of the MyApp class, each one would have certain
aspects of our MyApp.java code; however, some aspects would only exist one time in the JVM. Those
things that only exist one time per JVM are labeled static . Items that are static exist outside any
instance of MyApp and are shared by all. An interesting phenomenon that results from existence outside
an instance of a class is that those aspects of the class that are static are available even when you
haven't created an instance.
For example, the first line in my code could be this:
if( MyApp2.myRef == null ) {
In that line, I do not have an instance of MyApp2 that I'm referring to by name; rather, I am referring
to the class itself and testing the value of the static class member variable, myRef .
Now, this is precisely what we need to start running some Java code. By default, if I execute java.exe
and give it a class name to run, it will run the main() method. If I provide any arguments on the
command line when I execute java.exe , these arguments will be passed to the main() method as
parameters: an array of Strings . For instance, if I give this command:
java MyApp2 this is a test
Then there are four arguments passed to the main() method of MyApp2 : this, is, a, and test.
Here is the definition of main() from our code:
public static void main( String[] args ) {
After I describe the modifier public in the next section, you will know the meaning of that entire
definition. This specific syntax for the main() method is identical for any class that you want to run from
the command prompt (except that you can call the array of Strings , the arguments, anything you want,
not just args ).
We need to have a static method to start running Java code, because at that point we have not
instantiated any classes. Look again at the main() method in Listing 3-3 (repeated in the following). We
do something there that is quit typical. In the main() method, we instantiate an instance of the class
itself—we create a new MyApp2 instance, named m . To use the non-static methods of MyApp2 , we need to
call them within our instance of MyApp2 , so we call m.setRef() and m.getArray() . The m. prefix indicates
that we are calling the methods of our instance of MyApp2 named m .
public static void main( String[] args ) {
 
Search WWH ::




Custom Search