Learning Java/Objects and Classes: Difference between revisions

Content deleted Content added
m Replacing Level 1 headings with Level 2 headings.
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">
Line 19:
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...
 
===Fields===
Fields (also known as instance variables) are variables declared in the class itself - not in any method. For example, the following is a field:
<source lang="java">
Line 30:
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 visibility modifier they are defined with. In the example above "field" has an access modifier of private, which means that it may only be accessed from within the class itself, and may not be accessed directly outside of the class. We will touch on access modifiers later on.
 
===Methods===
You have used one method in each of your programs - main(String[] args). Methods are blocks of code which allow classes and objects to perform specific tasks. Methods are defined with an (optional) visibility modifier (such as public or private), an (optional) access modifier (final, static, abstract, etc), a return type (void, int, String, etc) which defines the type of value the method will give after being called, a name, zero or more parameters (inputs), and optionally a throws clause which defines exceptions which the method may throw (Exceptions are discussed later on.). Methods also contain a body, which contains any number of statements to be executed when the method is called.
 
Line 134:
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 type is a method that must be called for an object to be initialized. See Object for more information...
 
Line 164:
</source>
 
=== Nested Classes ===
Classes can also be added to other classes. These are called nested classes:
<source lang="java">
Line 178:
Nested classes act just like normal classes, but they cannot be called from other classes. Furthermore, they have access to all variables within the containing classes.
 
===Exercises===
{{Activity
| Classes #1
Line 226:
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.
 
==Objects==
Objects are basically prints of classes that can be changed - or else, instances of classes.
===Creating Objects===
Unlike some object oriented languages, objects are always allocated in the heap and are always created with a '''new''' operation. Object variables are '''references''' (similar to pointers, but with all of the dangerous parts removed), not instances, and can refer to any object of the declared type.
 
Line 267:
This is just like "int i=100", except that we are calling the constructor rather than using a primitive type.
 
===Using Fields and Methods===
You can also easily use methods/fields in a class. The syntax is:
<pre>
Line 288:
Each of these do the same thing except with a different string.
 
===Using Classes in One File===
If you create multiple classes in one file, you can make objects of them similarly.
 
Line 295:
<br /><br />
 
==Final Exercise==
 
{{Activity
Line 325:
 
{{CourseCat}}
[[Category:Pages with Level 1 heading]]