How can i move between different methods with the same class. When i programmed in VB, it was just the name of the subroutine, but i cant figure it out in java. Please put me out of my misery

Dani AI

Generated

Quick summary for : Java calls methods with parentheses and an execution context. If the helper is declared static (as your main is), it can be invoked directly from main. If the helper is an instance method, create an object and call it with dot notation (as mentioned). 's point about syntax is also relevant: method calls need the correct signature and matching braces.

Example — static helper (no arguments):

class ExampleStatic {
  public static void main(String[] args) {
    helper();    // direct call to a static method in the same class
  }

  static void helper() {
    System.out.println("helper executed");
  }
}

Example — instance helper (requires an object):

class ExampleInstance {
  public static void main(String[] args) {
    ExampleInstance animator = new ExampleInstance();
    animator.drawCanvas(600, 300);   // instance call via dot notation
  }

  void drawCanvas(int w, int h) {
    // drawing code here
  }
}

Common pitfalls and quick fixes:

  • Case matters: DrawCraneKitbox and drawCraneKitbox are different identifiers.
  • "non-static method cannot be referenced from a static context" happens when main tries to call a non-static method without an object — either make the method static or instantiate the class.
  • Method signatures must match: if a method expects parameters, supply them; if not needed, change the method to have no parameters. Avoid reusing args for unrelated methods to reduce confusion.
  • Follow Java naming conventions (lowerCamelCase for methods) to avoid mistakes and improve readability.

Troubleshoot by checking compile errors for exact messages, verifying method visibility and spelling, and deciding whether the helper really needs to be static or tied to an instance.

Recommended Answers

All 5 Replies

what do you mean "move between methods"?

You want to call one method from another? Check your textbook, see those braces?

Learn the language syntax and all will become clear, or at least clearer than it is now.

You use DOT notation (much like you do in VB I think.)

ClassInstanceName.MethodName(Optional parameters)

So if you have:

String myString = "Something";

// Then you could say:
System.out.println( myString.toUpper() );

'.toUpper' is a method of the String class that will output the string in upper case. :cool:

Opps, that was to call a method in a class.

Same way in VB. Use the method name. Just consider if you have a return value in the method, if so then you need to assign the return value to the same type.

Im still not sure. Heres the actual code

package animation;
import element.*;


public class Animation
{
public static void main (String args[])
{
int DrawingWindowWidth = 600, DrawingWindowHeight = 300;
ConsoleWindow c = new ConsoleWindow();
DrawingWindow d = new DrawingWindow(DrawingWindowWidth,DrawingWindowHeight);
//        Draw Environment



}


public static void DrawCraneKitbox (String args[])
{


}


}

im trying to call the DrawCraneKitbox method from within the main method.

Simple:

//Assuming you have a String Array (String args[]), lets call it:
String[] myStringArray = new String[2];

// And assuming you initilized myStringArray to somenting, you would have:
DrawCraneKitbox(myStringArray);

Since the Method was decalred as void, it has no return type. I would assume you want 'DrawCraneKitbox' to just do something. and then execute the next line of code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.