New Post has been published on heapload
New Post has been published on http://heapload.com/computer-science/java-classes-objects-methods-variables
Java Classes, Objects, Methods & Variables
Java Objects, Classes, Methods & Variables
Java Objects, Classes, Methods & Variables
Java classes represent properties and behaviors of any concept. It is a blue print used to define attributes and abilities of particular entity. For example, we want to define a car as a java class. We need to find out its functionality and components first. A car has wheels, breaks, accelerator, fuel, engine etc. All these can be the attributes of a car. A car can move forward, backward, right & left etc. these are the basic functionalities/methods for a car.
Java objects define a variable of a particular java class. Like a class, Java objects have states and behaviors. For example, we have a Mazda RX8 Sports Car and we want to code it. Before that, we need to define a class which has all the specs, then make a variable of that class and name it as Mazda Rx8 which is treated as an object of class car.
java-object-class-methods-and-variables
package javaexamples; /** * * @author Ahsan([email protected]) */ public class Car // Car is a class that represents basic structure of actual car. String model; // data attributes for a car. String type; int breaks, accelator, clutch; public void left() // methods that represents car's behaviour. public void right() public void move() public void stop() public static void main(String[] args) Car mazdaRX8 = new Car(); // mazdaRX8 is an object of class Car
Java Methods are the blue prints that hide all the implementations of any operation. We just need to call them with respective parameters, it will compute and return the required result. The main purpose of Java methods is to avoid coupling and code redundancy. It keeps source code concrete and easy to maintain. For example, we want calculate sum, difference, product and division of two numbers. One way is to write code that has two operands along with respective operators to compute results. But what if we want to do it several times. Notice you have used sample approach with different inputs. To tackle this, make a function and move all the statements in it, now you can call it as many times you want to.
package javaexamples; public class caculator public static int add(int op1, int op2) int result = op1 + op2; return result; public static int minus(int op1, int op2) int result = op1 - op2; return result; public static int product(int op1, int op2) int result = op1 * op2; return result; public static int divide(int op1, int op2) int result = (op2 > 0) ? op1 / op2 : 0; return result; public static void main(String[] args) // TODO code application logic here int op1 = 2, op2 = 3; System.out.println("Add :" + add(op1,op2)); System.out.println("Minus :" + minus(op1,op2)); System.out.println("Product :" + product(op1,op2)); System.out.println("Divide :" + divide(op1,op2));
Java variables are used to store some information that java programs use to compute results. They basically represent memory blocks with relative values. Java has some per-defined variable types like int, float, double, String, etc. but we can make variables of custom classes that stores information in specific manner like in car’s example.