Sie sind auf Seite 1von 6

What is a class? In real world, you will often find many individual objects all of the same kind.

There may be thousands of other bicycle and all of them are same. So heres the thing, all of the bicycle are built from same set of attributes, for example cadence, gear, break etc. So there is an existence of a blue print of each cycle before their existence in reality. In object oriented term, the blue-print of a cycle is called class and when the bicycle is made or built, we call it instance or object of the class or the blue-print. So in object oriented design, there must be a class before making an object. A class contains different components. A quick list of the components of a class 1. 2. 3. 4. 5. 6. 7. package statements import statements comments class declaration variables constructors Methods.

Now will discuss in details Package statement: All java classes are part of a package. If you explicitly define a package, then the class will be under that package otherwise it will be under default package. If a class is under default package, we dont need to declare package statement. But if you define a package, it should be the first statement of a class definition. For examplepackage com.codexplo.tutorial; public class Hello { } #A #B

#A package statement should be first statement in the class #B and rest of the class of Hello There are some naming conventions for java source code. Package sare usually defined using a hierarchical naming pattern. The levels are separated by (.) (pronounced dot). Package name must be in lower case. In general package name begins with the top level domain name of the organization where you are working on and the domain name of the organization and then the application name which are being written and then module name and so on. For example
com.codexplo.tutorial

Here, com-> top level domain codexplo- organization name, in my case its my blog site name. Not necessarily you have be in an organization, you may have own domain. tutorial here Im going to write a tutorial on java. So my application name is tutorial.

Import Statement: You cant write a java program without having help of other pre-written java classes. As you already know, java development kit has already a lot of libraries and classes written, we just use them in our own brand new class. So to get access of those classes in our own class, we need a way and the way is import statement. You will be probably heard about java swing library which enables us to develop cool user interface. There is class named JOptionPane which prompt user to take input and in some cases, provide information, lets forget it what exactly it does for now. So if we want to use JOptionPane in our source code, we need to import the package of the JOptionPane. For example
package com.codexplo.tutorial; #A

import javax.swing.JOptionPane; #B public class Hello { } #C

#A package statement #B Our import statement which enables us to access JOptionPane class. #C rest of the class body Comments: You can add comments in your java code. Comments can appear at multiple places in a class. A comment can appear before and after a package statement, before and after class definition, before and within and after a method definition. Comments are in tow flavors. Multi-Line comments: multi line comments span multiple lines of codes, they start with /* and end with */. For example

public class Hello { /* this class name is hello, is written for a demonstration of simple class component of java */ }

But most of the cases, when you read comments in java source code file, you will notice that it uses an asterisk to start the comment in the next line. Its not something mandatory, but looks more organized. Here is the example
public class Hello { /* * this class name is hello, is written for a demonstration of simple class * component of java */ } End of Line Comments: End of line comments start with // and it has no end tag. For example public class Hello { String firstName; // variable to store Person's first Name String lastName; // variable to store Person' last Name }

There is another flavor of comment profoundly used in different java source code before package declaration.
/** * @author Bazlur Rahman Rokon * @version 0.1 * * * class to demonstrate simple hello world program as we all know that traditionally * we start writing our first program in any language saying hello world * */ package com.codexplo.tutorial; import javax.swing.JOptionPane; public class Hello { }

Class declaration: The class declaration marks the start of class. Its a simple keyword class. A class can have access modifier, if the class is publicly accessible we add public keyword before the class keyword. Class can be 3

final if we need a class not to extend by any other class. We will discuss this part in any other lesson later. Heres the example of class
public class Hello { }

Class body, marked by the opening and closing curly brace, that is {}

Class definition: A class is a design, use to specify properties and behavior of an object. So a class holds two things properties and behaviors. Properties of a class are the variables and the behaviors of a class are the methods. For example a bicycle say it has speed, number of gears and cadence, these are properties. And the behaviors are to speed up, to change gears, apply breaks etc. Now lets design our class using this properties and behaviors.
package com.codexplo.tutorial; public class Bicyle { int cadence = 0; int speed = 0; int gear = 1; public void changeCadence(int newValue) { cadence = newValue; } public void speedUp(int increment) { speed = speed + increment; } public void changeGear(int newValue) { gear = newValue; } public void applyBrakes(int decrement) { speed = speed - decrement; } }

Few things to remember here: Class name starts with the keyword class. Java is case sensitive. So the words class and Class are complete different thing. You cant use Class instead of class. Class name must start with uppercase and if there multiple words in a name, next words also start with uppercase concatenating with previous one. No underscore (_) is allowed in java class 4

name. For example CourseName is valid class name. courseName or Course_Name are not allowed. Variables: already discussed about variable in the class definition part, again, variables are just properties of an object. Imagine human body, so variable may be your hearing limit; I mean you can only hear 20-20000 Hrs. . . . Its a variable. Variable names must start with lower case. And if there multiple words are needed to name a variable, next words will start with uppercase letter concatenating first one. For example firstName, lastName, dateOfBirth etc. Methods: methods are behavior of an object. See the bicycle class example. More simply what does an object do. As a human, we eat, drink, and talk etc. so these are the behavior of human. So if we write a class of a human, we must include these behaviors as methods. Example
public class Human { public void talk() { } public void eat() { } public void drink() { } public void laugh() { } }

Constructors: Constructors are special methods which is used to instantiate the class. Okay lets put it more simply So far we discussed about class definition and how the methods should be and so on. Lets think, we have class which has all the components required. Now somehow its needed to visualize the class, I mean we need to make it to give the class to an existence in the real world. How can I do it, well in java there is a special method which we call constructor, basically does this job. Every class has a default constructor. But as programmer we can have our own constructor. If you dont define your own constructor, the class will use its default constructor.

In the next tutorial well discuss the components in more detail. Stay tuned. And yes if you like my tutorial, dont forget to share it, like my Facebook page and follow me at twitter: facebook.com/codexplo and @ bazlur_rahman 5

Das könnte Ihnen auch gefallen