Learning Java/Objects and Classes: Difference between revisions
Content deleted Content added
→Fields: Cleaned up code example. Added further clarification. |
→Classes: Cleaned up code examples, major modifications. |
||
Line 1:
=Classes=
Classes are the core of Java. Everything is done within classes. You already know how to create a class:
<source lang="java">
public class NAME
{
}
</
You can also have multiple classes in a single file:
<source lang="java">
public class NAME
{}
Line 15:
class otherClass
{}
</
If you do this, only one public class is allowed and the others have to be protected-private (no modifier).
When this is compiled two class files will be created, NAME.class and otherClass.class. otherClass can be run by <code>java otherClass</code> just like normal. You can use these classes in objects...
Line 28:
}
</source>
Fields are like any other variable; they may hold primitive types (int, boolean, double, etc) or reference types (String, Integer, or any other class type). Fields can be accessed outside of the class depending on the
==Methods==
You have used one method in each of your programs - main(String[] args). Methods
Lets examine main again:
<source lang="java">
{
int i = 0;
i++;
}
</source>
Its visibility is public, which means it can be called outside of the class.
Its access modifier is static (more on this later).
It has a return type of void - it does not return anything.
Its name is main.
It takes one parameter, an array of String objects.
Finally its body contains two statements.
But what does that mean? Well, it means that it must have a return statement within it
<source lang="java">
public int returnsInt()
{
return 5;
}
</
This method has a single statement which simply returns the value 5.
Would the following work:
<source lang="java">
public void returnsNothing()
{
return;
}
</
The answer, actually, is yes because the return type in this case is "void" which means the method returns nothing.
Return statements terminate execution of the method, so any statements below the return statement will not execute. You can however place the return statement within a conditional block of code, so that if the condition is not met the block of code may be skipped and execution of the method will continue immediately after the conditional block. Consider the following code:
<source lang="java">
public void returnsNothing(int someNum)
{
if(
{
return;
}
}
</
If the parameter someNum equals five the method will exit. Otherwise it will continue to the next line of code immediately after the if block. A single line of code is executed in this case: <code>someNum = 1;</code>. This is a rather simplified example, however it illustrates how the return statement may be placed in various locations within the method.
Methods are quite simple to call, and you have already seen several examples of method calls in the previous lessons. The code below illustrates how to call a method defined within a class:
<source lang="java">
public class Ship
{
/**
* Instance variable which holds the speed of the ship.
*
*/
private int speed;
/**
*
* Default constructor.
*/
public Ship(int s)
{
speed = s;
}
{
public void setSpeed(int s)
{
speed = s;
}
public void accelerate(int s)
{
speed += s; //Add the value of s to speed and set speed equal to this new value.
}
}
public class TestClass
{
public static void main(String[] args)
{
Ship aShip = new Ship(5); //The ship now has a speed of 5.
ship.accelerate(5); //The ship now has a speed of 10.
}
}
</source>
The code above defines two classes: Ship and TestClass. Ship has a number of methods defined, including one called accelerate which adds an integer value to its speed. TestClass contains the main method in which we can create an '''instance''' of the Ship class. Once we have an instance we can then call its methods to manipulate its data. We do this by calling using the form: class/object.methodname(). The important thing to notice here is the use of the "." (dot) operator, which is how instance variables and methods are accessed in Java.
==Constructor==
The constructor of a
This method is different - its syntax is changed. Because it cannot return anything, it has no return statement. Also, the method name is the same as the class name. Here is an example:
<source lang="java">
class ConstructorTest
{
Line 104 ⟶ 147:
}
}
</
However, it CAN have parameters: <code>public ConstructorTest(String example)</code>
Parameters can be used to say what you want a field to be. Example:
<source lang="java">
class ConstructorTest
{
Line 119 ⟶ 162:
}
}
</
== Nested Classes ==
Classes can also be added to other classes. These are called nested classes:
<source lang="java">
public class cl1
{
private class cl2
{
}
}
</source>
A nested class can have one or more nested classes as well.
Nested classes act just like normal classes, but they cannot be called from other classes. Furthermore they have access to all variables from the same level and higher.
==Exercises==
Line 129 ⟶ 186:
Answer to #1.
<source lang="java">
public class Operation
{
Line 144 ⟶ 201:
}
}
</
Answer to #2
<source lang="java">
public class Operation
{
Line 160 ⟶ 217:
}
}
</
You may be wondering, if you did not have the "static" part, why your program did not compile. It is because "main" can only run static methods (except for constructors, which in a way are actually static). "static" makes a method OR field be the same even through objects (See Objects). You will get a better understanding of this.
|