Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Constant specific class body

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

I have a doubt about the "constant specific class body".

The compiler doesn't complaint with this code:



If I add the following new sentence in the main method, the compiler complaints:



If the constant specific class body only allows override methods, why the compiler doesn't complaint before with the setBrilliance method? Is there any way to use the setBrilliance method?

Thank you for your answer.
[ April 23, 2008: Message edited by: Marco Antonio ]
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try to complied the code.
I am getting errors when i tried to complied..

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Other.Colours cannot be resolved
Other.Colours cannot be resolved
Other.Colours cannot be resolved
Other.Colours cannot be resolved
Other.Colours cannot be resolved
Other.Colours cannot be resolved

at TestEnums.main(TestEnums.java:36)
 
Marco Antonio
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know what is your problem , but I obtain:

javac TestEnum.java
No compiler errors
java TestEnum
RED ---> 10
WHITE ---> 500
BLUE ---> 100

But, if I add the above sentence, I obtain:

javac TestEnum.java
TestEnum.java:37: cannot find symbol
symbol : method setBrilliance(int)
location: class Other.Colours
System.out.println(Other.Colours.WHITE + " ---> " + Other.Colours.WHITE.setBrilliance(0));
^
1 error


I haven't been able to invoke correctly the setBrilliance method. How can do it?

Thank you again.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Marco Antonio

i think we can not use void methods in System.out.println().
and in Colours enum there is no setBrilliance() so the compiler take that as just a method. so there is no compiler error.
if you try to invoke setBrilliance() in main() the compiler will say "cannot find symbol". since there is no setBrilliance() in Colours.
if you override that method that will work.

i think so. if its wrong, please tell me.

regards
sakthi
 
Marco Antonio
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hallo again and thanks for your answer.

It is true that a void method cannot be used in the System.out.println one. It has been a mistake, sorry! But if you use a method which returns a type in the provided scenario in a previous post, if this method is defined only in a constant specific class body and isn't defined in the enum, as long as you don't try to invoke this method, the compiler doen't complaint. And this is my doubt.

Why the compiler doesn't display an error?

I think that is possible to define methods in a concrete enum through the constant specific class body, that is why the compiler doesn't complaint, but I don't know how to do use of them.

Any idea?

Thanks for all.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i dont know if you need this still, but anyway, what happen is that the constant-specific class body is regarded as an anonymous (cannot be referred to by a name) class, so we can write whatever valid code inside the c-scb but the only exposed methods are wich are overriding an instance method, and the compiler doesnt complaint because the code is visible inside de c-scb even if you put it as public but not outside but only instace methods, ok???
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think here it is using overridding rules like as below



So here setBrilliance() is not defined in the enum class. this is what i think.
So correct me if i am wrong..
 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ELI Welcome to javaranch!

We have a rule here that you must not Wake The Zombies. And geeta you are right about the reason. Moreover the return type of setBrilliance is void, so I think it cannot be included in println statement even if it is declared in the enum ...
 
Greenhorn
Posts: 1
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I Dont Know If THis Is D Solution ...

Still ..

package defaultPackage;

import java.io.*;

class Other {
enum Colours {
RED(10), WHITE() {
// Was Used To Get Default Vale For White
/*
int getBrilliance(){
return 500;
}*/

// Can Be Used To Set User Specific Value Here ...
/*int getBrilliance() {
int i = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
try {
i = Integer.parseInt(br.readLine());
} catch (Exception E) {
System.out.println(E);
}
return i;
}
*/},
BLUE(100);

int brilliance;

Colours() {
this.brilliance = 200;
}

Colours(int _brilliance) {
this.brilliance = _brilliance;
}

int getBrilliance() {
return this.brilliance;
}

void setBrilliance(int BB) {
this.brilliance = BB;
}
}
}

class TestEnum {
public static void main(String[] args) {
System.out.println(Other.Colours.RED + " ---> "
+ Other.Colours.RED.getBrilliance());
System.out.println(Other.Colours.WHITE + " ---> "
+ Other.Colours.WHITE.getBrilliance());
System.out.println(Other.Colours.BLUE + " ---> "
+ Other.Colours.BLUE.getBrilliance());

// Simply Call set Brilliance To Set White Specific Value ..
Other.Colours.WHITE.setBrilliance(25);

System.out.println(Other.Colours.WHITE + " ---> "
+ Other.Colours.WHITE.getBrilliance());
}
}
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic