Author Topic: [Java]Multiple Functions, Static Functions, Declaring Functions as Objects  (Read 13977 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
From what I got, is a class like a recipe that defines how to do something or how something is made or what it is made of?

Also, I can only have one main function per program, how do I define which function runs first in a class if that class is not the class with the main function?

Also, why is the relation between file names and class names?

Another thing, a function in a class and both have the same name doesn't need a return type, why??

EDIT:

I reread your code and found out that functions with the same name of classes are called constructors!

So, the third and fourth questions are now useless.
« Last Edit: March 08, 2011, 09:59:31 am by Scout »

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
From what I got, is a class like a recipe that defines how to do something or how something is made or what it is made of?

Kind of. It's like an encyclopedia entry that defines the variables and functions associated with an object of that class.

Also, I can only have one main function per program, how do I define which function runs first in a class if that class is not the class with the main function?

You don't. In Java, classes do double-duty as programs if and only if they have a main function. Only one of your class files should have a main function; that's the one you run as a program. All the others are "helper" classes, like libraries.

Also, why is the relation between file names and class names?

What do you mean by file names? If you mean the name of the file you store the public class in, it MUST be the same as the name of the class (case sensitive). So for that Car example above, you must save it as Car.java.

If you mean the name of a function, you can name it anything that's a valid Java identifier (letters/numbers/_ only) and that's not a reserved keyword.

Another thing, a function in a class and both have the same name doesn't need a return type, why??

That's a constructor, which is a special type of function. It constructs an object when it's created. In other words, whenever you create a new object of that type, it gets run through the constructor which can set up/initiate the object.

Hope this helps.
« Last Edit: March 08, 2011, 10:40:15 am by Deep Thought »




Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Another thing, a function in a class and both have the same name doesn't need a return type, why??

That's a constructor, which is a special type of function. It constructs an object when it's created. In other words, whenever you create a new object of that type, it gets run through the constructor which can set up/initiate the object.

This is the one that I still don't get.

So when I make MainClass mainClass = new MainClass(); will it run through the constructor (if it exists?).

I also don't seem to quite understand private classes.

There can only be once public class per file, right? But there can be classes inside it, that are private. Are those classes like public classes?

I answered myself the last question:

Code: [Select]
public class MainClass {
public static void main(String[] args)
{
MainClass mainClass = new MainClass();
mainClass.MyFunction();
}

public void MyFunction()
{
System.out.println("Hello");
}
public MainClass()
{
System.out.println("How's it going");
}
}

Output is:

Code: [Select]
How's it going
Hello

EDIT:

A constructor is ran when its classed is called as an object??


Code: [Select]
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class MainClass extends JApplet{
public static void main(String[] args)
{
MainClass mainClass = new MainClass();
mainClass.theGUI();
System.out.println(Math.cos(100));
}

public void theGUI()
{
JFrame myFrame = new JFrame("Calculator");
myFrame.setSize(400,300);
JButton enterButton = new JButton("Enter");
enterButton.addActionListener(new DisplayMessage());
myFrame.getContentPane().add(enterButton);
myFrame.setVisible(true);
}
private class DisplayMessage implements ActionListener {
public void actionPerformed(ActionEvent e) {
            System.out.println("You just pressed a button");
        }

}
public MainClass()
{
System.out.println("This is the constructor class being ran.");
}
}

I made this piece of code to understand inheritance :D
« Last Edit: March 08, 2011, 04:45:05 pm by Scout »

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Another thing, a function in a class and both have the same name doesn't need a return type, why??

That's a constructor, which is a special type of function. It constructs an object when it's created. In other words, whenever you create a new object of that type, it gets run through the constructor which can set up/initiate the object.

This is the one that I still don't get.

So when I make MainClass mainClass = new MainClass(); will it run through the constructor (if it exists?).

Correct. Constructors are used to initiate new objects. For example, in that Car class I could have assigned the car a new Motor, a new Wheels[4], etc.

A constructor is ran when its classed is called as an object??

The class itself isn't an object. You create objects of that class.

Code: (Java) [Select]
Car myFirstCar = new Car("Ford", "Fusion");
Car mySecondCar = new Car("Mercury", "Milan");
Car myThirdCar = new Car("Lincoln", "Navigator");

would create three distinct Car objects. Then you can operate on each one individually:

Code: (Axe) [Select]
myFirstCar.drive();
mySecondCar.drive();    // Not going to crash my brand-new Lincoln :P




Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
I see Deep Thought!

I have another question, though:

I get this:

Code: [Select]
Exception in thread "main" java.lang.NullPointerException
at java.awt.Component.setLocation(Component.java:1997)
at MainClass.createGUI(MainClass.java:21)
at MainClass.main(MainClass.java:12)

When running this:

Code: [Select]
import javax.swing.*;

import java.awt.*;

public class MainClass extends JApplet {

JFrame mainWindow = new JFrame("Axe Sprite Editor");

    public static void main(String[] args)
    {
    MainClass mainClass = new MainClass();
    mainClass.createGUI();
    }
    public MainClass()
    {
   
    }
    public void createGUI()
    {
    mainWindow.setSize(640,640);
    mainWindow.setLocation(null);
    mainWindow.setResizable(false);
   
    JPanel pixel1 = new JPanel();
    mainWindow.add(pixel1);
   
   
    mainWindow.setVisible(true);
    }
}

I can't really understand why :S

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Well according to the traceback it's a "NullPointerException" in the line mainWindow.setLocation(null); in public void createGUI(), which means you're using a null where you're not supposed to. Function setLocation doesn't take a null, in other words.




Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
does this help at all? rarely should you call a method using null as the parameter.

you can do:
Code: [Select]
mainWindow.setLocation(x, y);
or
mainWindow.setLocation(new Point(x, y));


Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
But I've seen code centering windows aka frames using setLocation(null).

My goal is to center the window.

Code: [Select]
import javax.swing.*;

import java.awt.*;

public class MainClass extends JApplet {

JFrame mainWindow = new JFrame("Axe Sprite Editor");


    public static void main(String[] args)
    {
    MainClass mainClass = new MainClass();
    mainClass.createGUI();
    }
    public MainClass()
    {
    mainWindow.setSize(640,640);
    mainWindow.setResizable(false);
    JFrame pixel1 = new JFrame();
    mainWindow.add(pixel1);
    }
    public void createGUI()
    {
    mainWindow.setVisible(true);
    }
}

I also have this code but it seems like I can't add the pixel1 panel to mainWindow, so how do I do it?:

Code: [Select]
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Container.java:431)
at java.awt.Container.addImpl(Container.java:1039)
at java.awt.Container.add(Container.java:959)
at javax.swing.JApplet.addImpl(JApplet.java:300)
at java.awt.Container.add(Container.java:365)
at MainClass.<init>(MainClass.java:20)
at MainClass.main(MainClass.java:12)

Thanks much and sorry for disturbing you.
« Last Edit: March 12, 2011, 12:32:03 pm by Scout »

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
You can't add a JFrame to another JFrame like that.  In fact, you never really want to do that.  What exactly are you trying to achieve?

As for setting the window at the center, I believe it is setLocationRelativeTo(null).

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Oh damn, I just noticed, I made it JFrame, I meant JPanel. Sorry and thanks for the setLocationRelativeTo thing :)

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Oh damn, I just noticed, I made it JFrame, I meant JPanel. Sorry and thanks for the setLocationRelativeTo thing :)
no problem.  I've seen workarounds adding JFrames to other JFrames by doing some weird stuff, but it's usually not necessary.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Now from now on, I'm going to have problems in the layout of the window.

I wanna make a 640*640 window with 64 panels of 80*80 as in a grid.

It's a 8*8 grid, each square panel is 80pixels*80pixels, althogether makes a 640*640 pixels window.

Any suggestions/ideas/layouts/code on how to do this?
« Last Edit: March 12, 2011, 01:03:16 pm by Scout »

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Now from now on, I'm going to have problems in the layout of the window.

I wanna make a 640*640 window with 64 panels of 80*80 as in a grid.

It's a 8*8 grid, each square panel is 80pixels*80pixels, althogether makes a 640*640 pixels window.

Any suggestions/ideas/layouts/code on how to do this?
If you want it that accurate, just override the paint method and do it yourself.  Especially since there will be boundaries for any other object. (e.g. a frame around the box or panel or whatever you use).

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
The paint method, thanks, I'm gonna check on that.

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
yes, setLocationRelativeTo(null) is one of the few instances you pass null to a method.
and here's a simple demo that [maybe] draws the grid you want scout. i say maybe because i haven't tested it.