Java:Tutorials:Objects and functions
From GPWiki(Redirected from Java:ObjectsAndFunctions)
The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server. However, the GPWiki forums are still active! Come say hello.
[edit] Objects and Functions[edit] Object Oriented ProgrammingJava is an object oriented programming language. An object is a collection of variables and functions. An entire Java program is a collection of objects working together. [edit] ClassesBefore you can make an object, you must first make a class. A class is a blueprint for an object, it tells the program how to construct a class. An object is an instance of a class. [edit] Visibility of a classTo declare a class you first need to decide it's visibility, which can be either public, private, or omitted. If the class is public, then any class can create an instance of the class. There should be one and only one public class in each source file, and it should have the same name as the file. Eg if the class is called HelloWorld, then the source file it is in should be called HelloWorld.java. If a class does not specify visibility, then only classes in the same source file can make an instance of the class. Defining a class as private is only valid in an inner class. [edit] Declaring a classClasses are declared as follows: visibility class ClassName { // variables and functions go in here } Eg for a public class called HelloWorld: public class HelloWorld { // variables and functions... } [edit] Variables in classesA class can have many variables. Variables in classes also have visibility which can be either public, private, or protected. Public variables can be read and modified by any function, while private variables can only be read and modified by functions within the class. Protected variables act identically to private variables except when classes are being inherited. Public variables violate encapsulation, so all your variables should be private whenever possible. Variables are defined within a class exactly as they are in a function except their visibility must be defined. Any variable declaration follows the following pattern: visibility type name; Eg private int count; public double size; // public variables are bad, don't do this! protected float length; [edit] Static variablesA static variable is a variable whose value is the same for all instances of a class. A class can have many instances, each of which normally get their own copy of each variable. However, if a variable is static, then each instance of the class will share the variable. You can make a variable static by adding the static modifier, eg: private static int count; [edit] Functions(Functions are known as Methods in Java)Each class also contains functions. These functions provide a way for the rest of the program to communicate with the class and are the part of the class that actually do something. An easy way to think of a functions is as an action. A function is an action to be performed. [edit] Function visibilityFunctions also have public, private and protected visibility which control which functions can call them. Anyone can call a public function, while only functions in the same class can call private functions. Protected functions act like private functions except when inheriting. Since public functions are what the rest of the program uses to communicate with the object, they are often called the interface. Private functions are used to help the public functions do their work, so they are often called helper functions. [edit] ArgumentsFunctions can accept variables in the form of arguments. The arguments passed can be accessed by the function to be processed. For example, a function that prints some writing on the screen might take the string to be written as an argument. [edit] Returning variablesFunctions have the option of being able to return a variable when it has finished running. This may be the result of a calculation, or simply a status variable that indicates the success or failure of the function. The variable is returned using the return statement (see example below). If a function doesn't return a variable, then it has the return type void. [edit] Declaring functionsAll functions are declared within a class as follows: visibility return_type functionName(argument1_type argument1_name, argument2_type argument2_name, ...) { // Function body goes here } Eg for a fairly useless function that takes two numbers and returns their sum: public int addInts(int firstnum, int secondnum) { return firstnum + secondnum; } Or for a private function that doesnt take any values or return anything private void doStuff() { // Do things in here return; } [edit] Local VariablesFunctions can also declare local variables, these only last for the duration of the function. See Java:Variables. |


