Java says this method has a constructor name

32,080

You can't use the class name as the name for a method. The only "methods" that can share a name with the class are constructors.

One fix would be to rename your class from isPalindrome to PalindromeFinder or something. That would also better align with Java naming conventions.

EDIT: Note that you never actually called your method in main; you tryed to assign a local variable to isPalindrome. That does not actually call the method. You would need to invoke the method with isPalindrome(...put your parameters here...) and store the result in a variable with a name that isn't being used.

Also note that a method can only return a single value (a single primitive or a single object). If you really want to return an array AND a boolean (and I'm not sure you do), you would have to store those in an object and return that object.

Share:
32,080
lonesarah
Author by

lonesarah

Updated on March 02, 2020

Comments

  • lonesarah
    lonesarah about 4 years

    I want to return the value of my array plus the return value of the recursive call.

    However for something reason java does not want to have the method name after the constructor.

    In addition when i tried to convert the method into another method, i get error when i use isPalindrome.

    I change my program around but I'm still getting errors.

    
    public class isPalindrome
    {
        /**
         * This is the main entry point for the application
         * @return 
         */
        public static boolean main(String[] args) 
        {

    String[] word = {"KayaK", "Desserts, I stressed"}; boolean isPalindrome(String[] array, String s, String i) { if(i.charAt(0) == s.charAt(0)) { System.out.println("You entered a Palindrome"); return true; } else { System.out.println("You didn't entered a Palindrome"); } } try { System.in.read(); } catch (Throwable t) { } }

    }

  • Stephen C
    Stephen C about 13 years
    +1 - following the Java naming conventions will avoid this "problem" in its entirety/
  • lonesarah
    lonesarah about 13 years
    I understand taht but how will i return my array
  • lonesarah
    lonesarah about 13 years
    return isPalindrome(array, s.substring(1, s.length()-1)); is the problem
  • lonesarah
    lonesarah about 13 years
    I understand taht but how will i return my array
  • lonesarah
    lonesarah about 13 years
    I understand that but how will i return my array
  • Bala R
    Bala R about 13 years
    make the return type boolean[] instead of boolean I guess but I'm not sure about the parameters of that method.
  • lonesarah
    lonesarah about 13 years
    Would this work?? boolean isPalindrome = new boolean("true"); Boolean b = isPalindrome.booleanValue();
  • Michael McGowan
    Michael McGowan about 13 years
    Lower case "boolean" is a primitive type, not an object. Therefore you can't try to call a constructor by saying "new boolean(...)" and you can't try to invoke methods on them like "isPalindrome.booleanValue()".
  • Stephen C
    Stephen C about 13 years
    @user516805 - that's not even remotely valid Java syntax. And I can't see how it would help ... if it was.