• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Variable Arguments in Java 5

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends

I am unable to run the following program. I dont see any compile time errors but I get runtime error. The error which I get at runtime is

java.lang.VerifyError: (class: test/Enums, method: main signature: ([Ljava/lang/String V) Unable to pop operand off an empty stack
Exception in thread "main"

I am unable to debug the problem. Can anyone please help me, as to where I am going wrong ? I am using Eclipse 3.2 IDE, so dont know if I am missing out some settings. The only settings I checked for in eclipse is ensured that Installed JRE is Java 5 and compiler is refering to Java 5.

Waiting for your replies.

Thanks
Rohit.
***************************************************************************
package test;

enum Season
{
SUMMER(40),
WINTER(15),
RAINY(28)
{
int getHumidity()
{
return 90;
}
};

Season(int temperature)
{
mTemp = temperature;
}

int getHumidity()
{
return 50;
}

private int mTemp;
}
public class Enums
{
static Season s;

public static void main(String[] args)
{
System.out.println(Season.RAINY.getHumidity());
Enums e = new Enums();
System.out.println(e.varArgs1(1));
}

public void varArgs1(int... a)
{
for (int a1 : a)
{
System.out.println(a1);
}
}
}
***************************************************************************
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The code gives compiler error at the following line...

System.out.println(e.varArgs1(1));

varAgrs() returns nothing...its return type is void...so it can't be used
in a print statement..

Changing the method varArgs1() as follows:



Gives the output

90
1
0

as I would expect...

What output were you looking for?
Thanks,
Megha
[ April 30, 2007: Message edited by: megha joshi ]
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rohit Bhagwat:


I don't know why Eclipse isn't picking it up (it does for me on v3.2.1), but the varArgs1() method doesn't return any value, so you should be getting a compile error on the System.out.println() line.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On this line:


I get the compiler error "'void' type not allowed here" as expected. That's using Java jdk 1.6 compiler. And if you fix that by specifying a String to be returned by the method, then you had better return a String.

Eclipse 3.2.2 finds the same error.
[ May 01, 2007: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eclipse 3.1 with Java5 (eclipse's own compiler) also finds the problem with printing out a void.

Bu.
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Friends, I am very sorry. Indeed I am wondering how eclipse didnt gave me compile time error !! Because had eclipse given me compile time error, I wouldnt have been into so much of trouble of getting the answer as to why I am getting the following error.

java.lang.VerifyError: (class: test/Enums, method: main signature: ([Ljava/lang/String V) Unable to pop operand off an empty stack
Exception in thread "main"

This error has totally misguided me.. Indeed the following statement
System.out.println(e.varArgs1(1));
must give error, if the method returns void. Sorry for overlooking this statement.

Thanks all, for your replies, now I am able to see the output as expected...

Thanks
Rohit.
[ May 01, 2007: Message edited by: Rohit Bhagwat ]
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although Eclipse (and NetBeans) are very good for production coding, for the purpose of studying for the exam you should probably use the basic JDK. I have had a steady trickle of questions from people who find the results when using Eclipse differ from using a basic editor and the JDK.
[ May 01, 2007: Message edited by: Marcus Green ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic