The Artima Developer Community
Sponsored Link

Java Answers Forum
Switch statement and case variables

3 replies on 1 page. Most recent reply: May 16, 2002 6:12 AM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
herb slocomb

Posts: 2
Nickname: xman
Registered: May, 2002

Switch statement and case variables Posted: May 15, 2002 6:36 PM
Reply to this message Reply
Advertisement
I was trying to make a point that since constant variables did not have to be initialized when declared
they could be initialized at runtime and used to make the Switch statement more useful by using them as case labels. Well, sounds good in theory but I can't find anyway to make it work :

public class Switchtest
{

public static void main(String[] args)
{
int test = 0;
final int x;
x = 1;

switch (test)
{
case x: System.out.println("case x " );break;
default: System.out.println("Default ");break;
}
}
}

It gives an error about expecting x to be constant. I've tried static initializer blocks etc. Is there anyway to prove my point about having case label variables initialized at runtime?


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Switch statement and case variables Posted: May 15, 2002 8:28 PM
Reply to this message Reply
Hi
All case values must be a compile time constants. Java compiler makes sure that the all cases fall within the range of the variable used in switch statment. Therefore, a case cannot have runtime value assigned. This is stated in Java Language Specification. Therefore, if the compiler you are using implements JLS then you cannot prove your point.

Thanks
Kishori

herb slocomb

Posts: 2
Nickname: xman
Registered: May, 2002

Re: Switch statement and case variables Posted: May 16, 2002 5:04 AM
Reply to this message Reply
> Hi
> All case values must be a compile time constants.
> Java compiler makes sure that the all cases fall
> within the range of the variable used in switch
> statment. Therefore, a case cannot have runtime value
> assigned. This is stated in Java Language
> Specification. Therefore, if the compiler you are
> using implements JLS then you cannot prove your
> point.
>
> Thanks
> Kishori

Could there be some way to use a constructor to initialize those values ? For example different constructors could initialize the final variables with different values.

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Switch statement and case variables Posted: May 16, 2002 6:12 AM
Reply to this message Reply
Yes, you can initialize blank final variables, which have been declared as intance varaibel of a class, in construtors as in the following example. Still, you cannot use that variable in case of switch-case because it is not a compile time constant.

/////////////////////////////////
public class Switchtest {
	final int x; 
 
	Switchtest ( ) {
		x = 10 ;	// Inittialize x to 10
	}
	
	Switchtest ( int a ) {
		x = a ;	// Inittialize x to a
	}
 
	public static void main(String[] args) { 	
		Switchtest s = new Switchtest ( ) ;
	}
	
	public void demo() { 
		int test = 0;
 
		switch (test) {
			case x: // Still it is an error to use blank final variable x
				System.out.println("case x " );
				break;
			default: 
				System.out.println("Default ");
				break;
		} 
	} 
} 

Flat View: This topic has 3 replies on 1 page
Topic: IP-adres Previous Topic   Next Topic Topic: files

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use