0

I've a below code snippet.

public class GroupLayout {

    void setHorizontalGroup(GroupLayout.Group inp) {
    }

    public GroupLayout.ParallelGroup createParallelGroup(GroupLayout.Alignment.LEADING) { // Error is here
        return new GroupLayout.ParallelGroup();
    }

    public GroupLayout.SequentialGroup createSequentialGroup() {
        return new GroupLayout.SequentialGroup();
    }

    public enum Alignment {
        LEADING
    }

    class Group {

        void addGroup() {
        }
    }

    class ParallelGroup extends Group {}

    class SequentialGroup extends Group {}
}

I am trying to access LEADING constant from Enum type, but getting below error

Syntax error, insert "... VariableDeclaratorId" to complete FormalParameterList

May I know please what am doing wrong to access Enum constant?

Turing85
  • 18,217
  • 7
  • 33
  • 58
Squeez
  • 343
  • 2
  • 3
  • 15
  • You can't access the value in the **declaration** of a parameter. To declare a parameter, you need to specify a *type* and a *name*, like you did for `setHorizontalGroup`. – Andreas Apr 15 '18 at 15:33
  • @Andreas, Okay...Can you please explain more ? I'm not able to understand ...! :) – Squeez Apr 15 '18 at 15:34
  • 1
    Why is it that everyone uses soo many blank lines? Please do not do this. It does not improve the readability of your code. – Turing85 Apr 15 '18 at 15:34
  • Perhaps you meant for `createParallelGroup` to take an `Alignment` parameter, so the *caller* might specify `LEADING`? E.g. `public GroupLayout.ParallelGroup createParallelGroup(GroupLayout.Alignment alignment)`, and caller would then call it like this: `createParallelGroup(GroupLayout.Alignment.LEADING)` or simply `createParallelGroup(LEADING)` if they statically import it. – Andreas Apr 15 '18 at 15:37
  • @Andreas, So what's wrong in my code ? – Squeez Apr 15 '18 at 15:39
  • 1
    You use some specific value as formal parameter: `public GroupLayout.ParallelGroup createParallelGroup(GroupLayout.Alignment.LEADING)`. `GroupLayout.Alignment.LEADING` is not a type, but a specific value of type `GroupLayout.Alignment`. Just like `1` is a value of type `int`. – Turing85 Apr 15 '18 at 15:39
  • @Squeez *"So what's wrong in my code ?"* Read my first comment. I told you there what was wrong. – Andreas Apr 15 '18 at 15:41
  • @Turing85, I think when you said "Just like 1 is a value of type int", I'm able to understand "GroupLayout.Alignment.LEADING is specific value of type GroupLayout.Alignment". It hits me some where, but then what's a meaning of using like specific value of type "GroupLayout.Alignment.LEADING" and how can I use like this in my code ? – Squeez Apr 15 '18 at 15:46

1 Answers1

0

Your method definition syntax is wrong. A method can be defined with argument types but not their values, at least until Java will add support for default argument values.

You can either define an alignment argument:

public ParallelGroup createParallelGroup(Alignment alignment) {
    return new ParallelGroup(alignment);
}

or use the actual enum value inside the method body:

public ParallelGroup createParallelGroup() {
    return new ParallelGroup(Alignment.LEADING);
}

You can refer to JLS §8.4. Method Declarations for more information.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • Then how this code is working " GroupLayout gl_contentPane = new GroupLayout(contentPane); gl_contentPane.setHorizontalGroup( gl_contentPane.createParallelGroup(GroupLayout.Alignment.LEADING));" May I know please why its valid ? – Squeez Apr 15 '18 at 15:55
  • 2
    @Squeez you seem to confuse [formal and actual parameters](https://stackoverflow.com/questions/27494316/what-is-a-formal-parameter-in-java). – Turing85 Apr 15 '18 at 15:58
  • @Turing85, I got your point sir...But my concern is how can I use actual parameter in my code where its showing error ? Because in my last comment where you just replied, is the code is working right ...! – Squeez Apr 15 '18 at 16:02
  • @Squeez yes, because it is an actual parameter, not a formal parameter. As I said: `Alignment.LEADING` is a value of type `Alignment`. The formal parameter should be of type `Alignment`. At the calling side, the actual parameter must be a value of type `Alignment` (of which you currently only have `Alignment.LEADING`). – Turing85 Apr 15 '18 at 16:04
  • @Turing85, Okay, So where is wrong ? Whether I use Alignment.LEADING which is a type of Alignment or I define first formal parameter which should receive Alignment.LEADING at the end to get it working ...? No...? – Squeez Apr 15 '18 at 16:09
  • @Turing85, Tell me one thing, methodAcceptString("Java") or methodAcceptString(String inp)...where inp = "JAVA", What's a difference ? – Squeez Apr 15 '18 at 16:12
  • 1
    @Squeez one will compile, while the other won't. – Turing85 Apr 15 '18 at 16:12
  • @Turing85, Got it, I'm using value without formula ...! :) First formula needs to be placed and then value in it to make it work ...! Thanks man...! :) – Squeez Apr 15 '18 at 16:21