• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I access procedures from other classes?  RSS feed

 
Paul Carter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using NetBeans IDE and have just added an additional class to contain various procedures accessible via the main class.

In the additional class I've created a public procedure "public void SetUpGame()" , and I wish to call this in the main program.

My various attempts to access it however have failed e.g.
SetUpGame();
TowerCode.SetUpGame();

Do I need to import the class? I tried importing TowerCode.*, but had no joy there either.

Where am I going wrong?

Regards

Paul.



main class 'Tower':
package darktower;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Tower extends JPanel {etc. etc. etc.

Additional Class 'TowerCode' :
package darktower;

public class TowerCode {

/** Creates a new instance of TowerCode */
public TowerCode() {
}

public void SetUpGame() {
System.out.println("Set Tower Startup Values");
}
}
 
Renato Losio
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I tried importing TowerCode.*,

TowerCode is a class, not a package. you should import nameOfPackage.* or better nameOfPackage.nameOfClass

Cheers,

Renato
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"importing" is a red herring here. Your two classes are in the same package; therefore they are already available to eachother with no importing needed.

Note that Java doesn't have "procedures". It has methods, and methods are always defined in classes. That's the most important thing to remember: there's no such thing as a "free function" in Java. Every method is attached to a class or to an object.

The methods you've defined are attached to instances of the TowerCode class. Since both classes are in the same package, no importing is needed, but you do need to provide an instance of that class as the context for invoking those methods. So, for example, the following would work:

TowerCode tc = new TowerCode();
tc.setUpGame();

Finally, though, I want to point out that although in a procedural language, this might be a sensible way to organize things, it's not really the way things are done in an object-oriented language. Methods should be defined in the class where the data they operate on is. By your own confession, TowerCode is "an additional class to contain various procedures accessible via the main class". That is no justification for creating a new class. If you need a class to manage a database or high scores, or a class to represent a monster, or a class to represent a game board or a data file or anything else along those lines, then yes, create a new class. But don't just put code into different classes willy-nilly. Let the code follow the data.
 
Paul Carter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers guys

Been using Delphi for a while and thought it would just be a matter of learning the equivalent syntax. Java is sooo different however in these early stages I feel like I'm scratching around in the dirt

Plus although Object Orientated, Delphi holds your hand so much you don't really have to think about it.

I guess it's time to get my head round some real OOP.

Best regards

Paul.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!