user2025915
user2025915

Reputation: 3

how to define an array as argument in class constructor?

I want to ask what is the proper way to define an array as argument in class constructor ?

for example

public class example {
    int x;
    int y;
    String [] x = new Array [5];

    public example(int x, int y, String [] x){
    etc etc etc.

Is String [] x is written right ? because netbeans is giving an error however i change it.

Upvotes: 0

Views: 222

Answers (3)

Danyel
Danyel

Reputation: 2250

String [] x = new Array [5];

should be

String [] x = new String[5];

And your variable x is a duplicate. You should change that.

Also, class names start with an upper case letter by convention.

You should also work on your formatting / indent for easier readable code.

Upvotes: 0

Alex
Alex

Reputation: 94

You can define the array as such:

 String[] x = new String[5];

edit: and make sure you don't repeat variable names so change x to something else as you have defined x as an int already.

Upvotes: 0

hsz
hsz

Reputation: 152294

It is the proper way, but you have repeated x variable name. Change second x to z for example.

Upvotes: 1

Related Questions