might be main or another method // dont know yet

int[] num1 = new int[intA];
int[] num2 = new int[intB];


public static void cal(what do i put here)

blah blah

and

class Homework7v2
{
int a=0; // i need to use this int all the way through and
// it wont let me put it here>?
public static void main(String[] args)

Dani AI

Generated

Short answer: declare the method to accept the types you need (int arrays and/or the integer a), or call a non‑static method on an instance so it can access an instance field. As pointed out, a method signature must list parameter types; for arrays use int[] and you can also return an int[] if needed.

Example — static helper that receives two arrays and the integer:

public static void cal(int[] arrA, int[] arrB, int aValue) {
    // operate on arrA and arrB; changes are visible to the caller
}

Call it from main by passing references:

int[] arrA = {1,2,3};
int[] arrB = new int[5];
int aValue = 10;
cal(arrA, arrB, aValue);

Example — instance method that uses a non‑static field a:

public class Homework7v2 {
    int a = 0; // instance field

    public void calInstance(int[] x, int[] y) {
        // can read/write this.a here
    }

    public static void main(String[] args) {
        Homework7v2 hw = new Homework7v2();
        hw.calInstance(new int[]{}, new int[]{}); // call via instance
    }
}

Troubleshooting tips:

  • You cannot use a non‑static field from a static context without an instance; either make the field static, pass the value as a parameter, or call a non‑static method on an instance.
  • Arrays are reference types: a method can modify the array contents and the caller will see those changes. If you need an independent copy, use Arrays.copyOf(...).
  • Watch for NullPointerException (ensure arrays are initialized) and IndexOutOfBoundsException (validate indices).

Further reading: Java methods and parameter rules, and arrays behavior in the Java Tutorials: Methods and Arrays.

Recommended Answers

All 3 Replies

Actually in a program (application) you will only have one main method.
All others are just classes (with no main method).

Same as in VB, you only have one 'Sub Main' routine.

i ment to another method as below

"public static void cal(what do i put here)"

any ideas?

If you are familure with VB, it is simular.

In your declared method, you declare the types of variables you wish to receive, like this:

// The below method (MyMethod) is passed two parameters, 
// one is a Integer, and another is a String
// It also RETURNS a character value
public char MyMethod(int myInt, String myString) {
    char someChar = 'A';  
    // someChar is my return value (which can be modified in the method)
    // You can then use myInt, and myString in your method
    //  ... Some other processing
    return someChar;
}  // End of MyMethod

Now to call this your would code:

// Somewhere in your program (can be in the main method, 
// or can be called in another method too)
returnChar = MyMethod( 1, "test string" );

// Noticed we passed an integer, and a string value.

Note - that 'returnChar' is declared somewhere in your program (mose times at the begining of the main, or it can be declare in the class or the method in which it is used. It just depends on how you wish to use 'returnChar' (look up scope.)

Also, you can have methods that do not return a value. In this case the above MyMethod would look like this:

// The below method (MyMethod) is passed two parameters, 
// one is a Integer, and another is a String
public [B]void[/B] MyMethod(int myInt, String myString) {
    char someChar = 'A';  // My return value (which can be modified in the method)
    // You can then use myInt, and myString in your method
    //  ... Some other processing
    // NO RETURN VALUE NEEDED
}  // End of MyMethod

Using the void return type you would not need to assign to a value. So you would just have:

// Somewhere in your program (can be in the main method, 
// or can be called in another method too)
MyMethod( 1, "test string" );

Lastly, you can have any number of parameters (or non at all,) just separate them with a coma. And don't forget to declare the type of parameter passed (since Java is a strongly typed language.)

Good Luck! :D

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.