13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

58Chapter 3An Experienced Programmer’s Introducti<strong>on</strong> to Javaaddresses, and so <strong>on</strong>. Java is an object-oriented programming language and thushas some significant syntax related to OO c<strong>on</strong>cepts.If you are new to object-oriented programming, be sure to read Chapter 1of Eckel’s Thinking in Java.In Java, we define a class to represent the objects about which we want toprogram. A class c<strong>on</strong>sists of the data and the methods to operate <strong>on</strong> that data.When we create a new instance of some class, that instance is an object of thattype of class. Example 3.5 shows a simple class.Example 3.5 Simple classclassPairInt{// dataint i;int j;}// c<strong>on</strong>structorsPairInt() { i=0; j=0; }PairInt(int ival, int jval) { i=ival; j=jval; }// methodssetI(int val) { i=val; }setJ(int val) { j=val; }int getI() { return i; }int getJ() { return j; }Note that this class defines both data (i, j) and methods (setI(),getJ(), and so <strong>on</strong>). We put all this into a file named PairInt.java to matchthe name of the class definiti<strong>on</strong>.If some other Java code wanted to create and use a PairInt object, itwould create it with the new keyword followed by a call to a c<strong>on</strong>structor(Example 3.6).This example shows <strong>on</strong>ly a snippet of code, not the entire PairInt class.That class, though, would likely reside in its own source file (named for its classname). In Java you normally create lots of files, <strong>on</strong>e for each class. When it’s

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!