2

I am really a beginner in Java and I would be grateful if someone could explaine me how can I get a return from my method in order to use it in another class from where I actually call the following one. My code is:

private static String[] months(int val){

    String[] monthsNames = { "January", "February", "March", "April", 
            "May", "June", "July", "August", "September", "October", 
            "November", "December"};

                   return monthsNames[val];
}

edit: I did

public static String months(int val){

    String[] monthsNames = { "January", "February", "March", "April", 
            "May", "June", "July", "August", "September", "October", 
            "November", "December"};

                   return monthsNames[val];
}

and what I get from eclipse is that monthsNames cannot be resolved to a variable

edit2** now it works. Thanks everybody for your help!

1
  • 3
    I am really struggling to find the question :( Commented Jul 9, 2016 at 21:08

4 Answers 4

4

You are returning one String, not a String[]. Change

private static String[] months(int val){

to

private static String months(int val){

But, if you want to call it from another class you will need to change private to public (or if the other class is in the same package, you could remove private and then you would have package-private level permission). Finally, if the other class is a sub-class, you might change private to protected (and then it is only visible to sub-classes).

Sign up to request clarification or add additional context in comments.

1 Comment

Well i changed to private stating String Months(int val) {...} but eclipse says that This method must return a result of type String. So i place inside the method return monthsNames[val]; and its totally wrong . What can i do?
4

Making the method private restricts the visibility to the class in which the method is declared. If you want to use this method in another class you should make it more visible, for example by declaring it public.

Secondly your method is currently returning an array of String (String[]). If you want to return a String you should use public static String months(int val) instead of public static String[] months(int val)

If the method returned the monthsNames variable, String[] would be correct, however the elements of monthsNames are just String.

public static String months(int val) {
    String[] monthsNames = { "January", "February", "March", "April", 
            "May", "June", "July", "August", "September", "October", 
            "November", "December"};
    return monthsNames[val];
}

Comments

2

Change the return type, you want to return the string when given the index...

private static String months(int val){..

Comments

2

If you want to return months Name from monthsNames just change the return type and access modifier.

private static String[] months(int val); 

to

public static String months(int val);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.