7 Replies - 348 Views - Last Post: 04 January 2013 - 11:55 AM Rate Topic: -----

#1 dulvinrivindu  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 51
  • Joined: 25-September 12

varargs error. I cant assign values to the array.

Posted 01 January 2013 - 08:48 AM

public class Exampleprg {
	Exampleprg(String... person){
		for(String name: person){
			System.out.println("Hello " + name);
		}
	}
	public static void main(String args[]){
		Exampleprg("john","david", "suhel");
	}
}


It tells "Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method Exampleprg(String, String, String) is undefined for the type Exampleprg

at Exampleprg.main(Exampleprg.java:9)"

Is This A Good Question/Topic? 0
  • +

Replies To: varargs error. I cant assign values to the array.

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,234
  • Joined: 21-June 11

Re: varargs error. I cant assign values to the array.

Posted 01 January 2013 - 09:37 AM

On line 2 you're defining a constructor of the class Exampleprg - you're not defining a method named Exampleprg.

Note that this is not related to varargs. You'd have the same problem if you defined Exampleprg to take three strings as arguments.
Was This Post Helpful? 0
  • +
  • -

#3 ShawnaInMaine  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 05-May 11

Re: varargs error. I cant assign values to the array.

Posted 01 January 2013 - 09:40 AM

What I have learned about arrays is that they have to use square braces []

Something to this effect will work
static String[] names = {"John", "David", "Suhel"} ;//this declares the array of names
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {


System.out.println("Hello " + names[1]);//this will print hello and the first person name in array (which in example above is david because john is 
0 in array
    }
}


This post has been edited by macosxnerd101: 02 January 2013 - 01:03 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,234
  • Joined: 21-June 11

Re: varargs error. I cant assign values to the array.

Posted 01 January 2013 - 10:33 AM

View PostShawnaInMaine, on 01 January 2013 - 05:40 PM, said:

What I have learned about arrays is that they have to use square braces []


There is nothing wrong with OP's code other than what I explained in my earlier post.

Variadic methods are defined with TypeName ... as the type of the last parameter - not TypeName[]. If you use the latter, you end up defining a regular method taking an array as its argument - not a variadic method. Such a method would need to be called with an actual array as its argument and thus could not be called in the way the OP intends.
Was This Post Helpful? 0
  • +
  • -

#5 dulvinrivindu  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 51
  • Joined: 25-September 12

Re: varargs error. I cant assign values to the array.

Posted 01 January 2013 - 08:23 PM

View Postsepp2k, on 01 January 2013 - 09:37 AM, said:

On line 2 you're defining a constructor of the class Exampleprg - you're not defining a method named Exampleprg.

Note that this is not related to varargs. You'd have the same problem if you defined Exampleprg to take three strings as arguments.


So how shud I recorrect this sir. I mean i m new to this varargs sstuf. I saw this in a book that im using.
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: varargs error. I cant assign values to the array.

Posted 01 January 2013 - 10:36 PM

Like sepp2k said, your problem is with how you are calling the constructor. That Exampleprg(String... person) is not a regular method, it is a constructor because it has the same name as the class with no return type.
To call a constructor you have to precede it with the keyword new which means we are instantiating an object and a constructor will be called automatically:
 new Exampleprg("john","david", "suhel");

Was This Post Helpful? 2
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,234
  • Joined: 21-June 11

Re: varargs error. I cant assign values to the array.

Posted 02 January 2013 - 12:44 PM

View Postsmohd, on 02 January 2013 - 06:36 AM, said:

To call a constructor you have to precede it with the keyword new which means we are instantiating an object and a constructor will be called automatically:
 new Exampleprg("john","david", "suhel");


However, as it doesn't look like it was intended to be a constructor and it certainly shouldn't be one from a design point of view, it would make more sense to turn it into a (static) method and keep the method call unchanged (i.e. no new).
Was This Post Helpful? 1
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: varargs error. I cant assign values to the array.

Posted 04 January 2013 - 11:55 AM

I guess this is what you try to do
Would be a lot clearer if you do not name a method the same as the name as the class

public class Exampleprg {

	static void foo(String... person){
		for(String name: person){
			System.out.println("Hello " + name);
		}
	}
	public static void main(String args[]){
		foo("john","david", "suhel");
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1