Computersight > Programming > Java

Basic Java

Learn the basics.

The “Instincts” of Java

Methods are the commands or objectives that are associated with classes or objects. They're a bunch of statements stringed together with a given name that execute/ perform an action when you call upon it. Methods are used mainly for to provide a way to access the private data stored in a class or object. An example of this is stated down below.

public class Example

{

public void Attack

{

//method here

int life

int hit=10

life= 100-hit

}

}

The “Genetic” Makeup of an Object

Member data or instance data as it's sometimes called give an object its variables creating the type of space it can be; in such as a flashlight has a bulb. The more dictionary-like term is data encapsulated within a class or object. It retains a unique value for each instance variable in the class. These gives an object or class its “genetic” values such as if the subject you're working on is cars, you will have member data relating to cars and not cards. An example of a member data is a monster's HP for it is a unique part of all monster games.

The Life Machine

A constructor in a class is basically a group/block of statements called upon when an object is declared. A constructor is similar to class methods but not quite they don't have a return type and have different rules for modifiers. They are used when an object is being created and also usually have the same name as the declaring class. The job that constructors do is to pre-define the object's member data and to establish an unchanging condition of the class. It's like having a manufacturing machine with specific instructions on what to do. Below is an example of a constructor. As you see it names off member data and helps set up values for them. When an object is made the data in the constructor is used as a base.

Note this is only part of the program not including the first statements to initialize a file.

public class Degrees

{

public Degrees

{

int room temp=80

int hot temp= 105

int cold= 0

int ok= 70

how= new String (“How hot is outside”)

//ect…

}

}

Creating an Object

To create an object you must use the new keyword. Using this word will use the constructor you have set up (hopefully). In the parameters you should give a value for your member data in listing order as your constructor or leave it blank if your values are set and won't change. Example is shown below: the 1st card gives the statement what type of object you are creating. The three is just a name for the card you created. Last inside the parameter is the #9; giving the card value face of 9 (pretend in my constructor I had a member data showing what # the card is.

Card three= new Card( 9);

1
Liked It
I Like It!
Related Articles
The Java Platform  |  The Basics of Java Programming [1]
Comments (0)
Post Your Comment:
Name:  
Copy the code into this box:  
Post comment with your Triond credentials?
Inside Computersight

Communication & Networks

 /

Computers

 /

Hardware

 /

Operating Systems

 /

Programming

 /

Software


Popular Tags
Popular Writers
Powered by
Computersight
About Us
Terms of Use
Privacy Policy
Services
Submit an Article
Advertise with Us
Contact

© 2007 Copyright Stanza Ltd. All Rights Reserved.