View Full Version : Very basic terms & structure help

Eternity Angel
03-20-2008, 08:48 PM
I use PHP, HTML & JavaScript to make games... well, all my newest ideas would be performed better if they had a way to load, store, and alter graphics on the fly. I looked into Flash, and besides it being expensive for the development software, I just could not make it work (a friend gave me an install awhile back, but if I could have made it work, I would have bought my own). So, I turned to Java.

What I need is a VERY basic guide for explaining everything, or some help here for it.

My questions so far are very basic;

1.) I am used to FUNCTIONS - I can have as many functions per document as I want. When I try to compile hello world examples, they work only if the .java file is named the exact same as the "class" declaration inside it. Does this mean that I can only have one 'class' per file? And if so, how would that even work? Are classes not like functions?

2.) What are all the (what seems to be) unnecessary keywords in classes and stuff? "public class SomeClass extends Applet" -- what is "public"? "extends"? "Applet"? Inside the class, "public void init()" -- what would the 'void' be for?

3.) Is the first class (or if you can only have one per file, the only one) the one that is called when the applet loads...? I don't understand a class being called without me putting something like "$blah = SomeClass(para,meters);"

4.) For the hello world example I have, this is used: "public void paint(Graphics g)" -- is "Graphics" a type or something? Is "paint" something that I can change, like a function within a function?

To be honest, I feel like giving up and working out some sort of JS thing instead, since all of these things are stopping me from understanding what is going on and what I am doing enough to move on and learn more. I rarely can just accept that you need to do X for Y to work -- I need to know WHY you need to do X.

I have never worked with any form of REAL programming language, so if these things are standard across programming languages, then I'm sorry for being naive. Maybe I am just too used to PHP and JS babying me.

03-20-2008, 09:36 PM
The first thing I would recommend is to pick up a book on Java, as you will find that there is a lot more depth here. To answer your questions...

1. I think you are making the wrong comparison for functions. Functions in other languages would more closely equate to "Methods" in Java. Classes are core to Object Oriented Programming and should define some sort of Object. Here is an example of a simple Java program that you might find in a textbook:

public class Rectangle
private int length;
private int width;

public Rectangle(int length, int width)
this.length = length;
this.width = width;

public int getLength()
return length;

public int getWidth()
return width;

public int getArea()
return length * width;

And here is a Class that would use this Rectangle "Object" to create a rectangle and print its area

public class MakeRectangle
public static void main(String[] args)
Rectangle rect = new Rectangle(5, 7);

In this example, the first class represents a Rectangle Object, and it defines 2 variables that can be thought of as attributes of this object, the length and width. Then we have a constructor, which is what is used to create the object, defining the length and width. Following this, we have 3 methods(similar to functions as you are used to) that perform some type of action that you might do with a rectangle. In this case, I've given it the ability to tell you its length, its width, and its area, using 3 different methods.

2. The keywords you see all have an important meaning:

public/private/protected : This defines the types of classes that have access to this object. For example, private variables as in my above example, can only be accessed from within the same class in which they are defined, which is the purpose of the getLength and getWidth methods.

extends : This is key for inheritance, which is one of the most important concepts in Object Oriented Programming, an example:
In the above example, a Rectangle is a type of shape, so we could define a shape class, with some basic attributes that apply to ALL shapes, and create a Rectangle, Circle, etc.. class that "extends" the shape class, thus inheriting its attributes.

void : This is used as a return type when a method does not return a value, it is the same as my example using public int getLength, where int is the return type.

3. A class that has a public void main method is usually where an application starts, or an init method in the case of an applet. Java classes are called and used as in the above example by making a reference to them such as Rectangle rect = new Rectangle(3, 5), this creates a new rectangle, by passing 3 and 5 as arguments to the Rectangle constructor, and the variable rect is used as a reference to this new object.

4. In your example, Graphics is another java class that defines functionality for graphics. The definition for the paint method is saying that the paint method accepts a Graphics object as an argument, so that you can perform the actions defined by it, from within your paint method.

Eternity Angel
03-21-2008, 12:13 AM
I still don't get the classes then - I define a class, which I can only have one per file, then how would I use it in another class...? Do I need some sort of include() function in my main .class file?

03-21-2008, 03:23 AM
You can have multiple classes in a file, but they are used exactly how I showed in my example. The example assumes that the .class files are in the same directory. If they are not in the same directory, you will need to look into import statements and placing the classes on the java classpath.