java_vars_methodsx

Download Report

Transcript java_vars_methodsx

Java: Variables and Methods
By Joshua Li
Created for the allAboutJavaClasses wikispace
Primitive Variables
Primitive variables are variables of the most basic type, and the most
commonly used primitive variables are the following:
int – This is an integer. Use this type of variable to store integers ranging from
-2,147,483,648 to 2,147,483,647. If you need to store larger numbers, use
primitive type long. Google java long for more info.
boolean – This represents a true or false value. Use this type of variable to
make conditions that can be set and changed from true to false and vice versa.
char – This represents a single uni-code character, like ‘a’. This variable is
useful for initials or hotkeys.
Primitive Variables: Declaration
To declare a primitive variable, the syntax is:
(modifiers)(access-modifier)(static or non-static)(variable type)(variable
name);
An example: (This is declaring a private and static variable called
numberOfPillows of type int)
private static int numberOfPillows;
If you want to assign a variable a value during declaration, simply do this:
private static int numberOfPillows = 9;
Classes: Runner Classes
As you may know, every program you create is called a class. Normally,
classes don’t have any executable code, that is, the code in a class will not
produce output of any sort.
The classes made to run programs are called runner classes and have a main
routine. A class that displays Hello World in output goes like this:
public class HelloWorldClass
{
public static void main(String[] args)
{
System.out.println(“Hello World”);
}
}
Classes: Runner Classes Cont.
Let’s break this down.
public class HelloWorldClass This declares a class called HelloWorldClass.
public static void main(String[] args) This is the main routine. Don’t worry
about all the void and args and String[], you will understand that later.
System.out.println(“Hello World”); This prints out the message Hello
World.
In short, Runner Classes have a main routine that executes all the good
stuff.
Methods: Custom Methods
You may have used some of Java’s built in methods, such as the
println() method, which usually takes a String or an object
parameter.
But the people who wrote Java can’t write everything, can they?
Like many other programming languages, java, gives the
programmer total control, that is, allowing you, the
programmer, to create your own methods and use them.
A custom method can either be a method that needs input
(parameters), or a method that just performs an action.
Methods: Examples
To make your own custom method, the syntax is as follows:
<modifiers> <return-type> <method name> (<params, if any>)
{
<code and return statement (if needed)>
}
An example of a method that adds 7 to a int passed into it:
public int addSeven(int parameterInt)
{
return parameterInt + 7;
}
Methods: Implementation
Using custom methods is just like using Java methods, but remember
that addSeven() returns an int. Let’s say that you wanted to use
addSeven in a main routine to add 7 to an int variable…(this is not
practical, i’m just using it as an example)
public static void main(String[] args)
{
int myNum = 4; //Declare myNum and set it equal to 4.
myNum = addSeven(myNum);
//In here, you return myNum + 7 and assign it to myNum.
//At the end of this code, myNum equals 11.
}
Static Methods and Variables
Static means only one copy, that is, can only be used inside the
class. Static methods can be accessed like so in an outside class:
<name of class which contains the method>.<method
name>(<params if any>)
Like: Math.sqrt(9);
Static variables are accessed the same way:
<name of class which contains the variable>.<variable name>
Like: myAnimalClass.numberOfDogs;
Classes: Object Classes
There is another type of class, and I like to call it the object class.
This is a class that programmers create in which they want to use it
to create variables with different properties, which we will explain
very soon.
In object classes, there are things called constructors, which are
basically methods that are used in the creation of a new object with
the type of your class.
The syntax for a constructor goes like this:
<modifiers, usually public> <class name> (<params, if any>)
{
<code, usually code to save the parameters to variables
inside the class>
}
Classes: Object Classes Cont.
Let’s say you want to make an object class called Car.
Cars have wheels, doors, weight and color. We will use those in
the constructor when writing our Car class.
Classes: An Example
Here is the source code the object class mentioned earlier: Car.
public class Car
{
int numWheels, numDoors, amtWeight;
String typeColor;
public Car (int wheels, int doors, int weight, String color)
{
numWheels = wheels;
numDoors = doors;
amtWeight = weight;
typeColor = color;
}
}
As you can see, everytime you create a new Car, the values you pass into
the constructor as params will be saved into the class (or non-static) variables
defined in Car. What are class variables? I will discuss that very soon.
But how do we create a new Car? Hmm…
Object Variables
You can make your own variables and make those variables’ types your class, as
we know from Object Classes.
Let’s take the previous example, the object class Car. We will use its constructor
to create a new object variable with type Car!
This is how you create an Object variable. It follows most of the original syntax
to create a new variable.
private static Car myHonda = new Car(4,4,2250,”Beige”);
The only difference is the new Car(). new calls the constructor to do its work in
the Car class. After this, you have a Car variable named myHonda, and it has 4
wheels, 4 doors, weights 2250 pounds and is Beige colored!
Class (Non-Static) Variables and Methods
The last thing that you need to know are class (or non-static variables and
methods), and knowing this will wrap things up.
Static means one copy, so non-static must mean more than one copy. When you
declare a variable or a method and you do not put static, that makes it a class
variable/method. This means that each time you create a object variable of that
class like Car, there will be a copy of the class variables and methods for each
Car that you create!
To summarize, static variables and methods can only be used in the class, and
can be accessed outside of the class by going like <class name>.<variable
name/method>. Class variables and methods CANNOT be used in the class,
and can only be used when a object variable (or just object) of that class is
created.
That’s All!