Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

SCJP study guide for java 6 Error in Chapter 1

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
I've just bought the <SCJP study guide for java 6> and have finished the self test of chapter 1. I think there's an error for question 4 :
<blockquote>code:
<pre name="code" class="java">
enum Animals{DOG("woof"), CAT("meow", FISH("burble");....}
Class TestEnum{
static Animals a;
public static void main(...){
System.out.println(a.DOG.sound+" "+a.FISH.sound);
}
}
</pre>
</blockquote>
the answer is this code will work?! I don't think we can access a static field like this(it should be Animals.DOG no?)
[ July 11, 2008: Message edited by: Zheng ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For sure this code works. You can access a static member of a class by a reference to it (DOG, CAT and FISH are public, static and final instances of Animals class). I hope this helps.
[ July 11, 2008: Message edited by: Jarek Jankowski ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DayLight ISIMA, please check your private messages for an administrative JavaRanch matter. You can see them by clicking My Private Messages.

Also, please use code tags when you post code.

About your question: There's an error, you put a ) in the wrong place. Did you try the code out? That's the quickest and easiest way to find out if it works.
[ July 11, 2008: Message edited by: Jesper Young ]
 
Zheng Wangz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jesper Young:
DayLight ISIMA, please check your private messages for an administrative JavaRanch matter. You can see them by clicking My Private Messages.

Also, please use code tags when you post code.

About your question: There's an error, you put a ) in the wrong place. Did you try the code out? That's the quickest and easiest way to find out if it works.

[ July 11, 2008: Message edited by: Jesper Young ]



It has been done. And I've tried it. An error message in eclipse :
the static field Animals.DOG should be accessed in a static way.

So I want to know it's an error or I've missed something
[ July 11, 2008: Message edited by: Zheng ]
 
Zheng Wangz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jarek Jankowski:
For sure this code works. You can access a static member of a class by a reference to it (DOG, CAT and FISH are public, static and final instances of Animals class). I hope this helps.

[ July 11, 2008: Message edited by: Jarek Jankowski ]



I've got an error message which I described above...
And I think that a.DOG make no sens in OOP. DOG isn't a field for object.
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Zheng,

generally, while studying for SCJP, it's better not to use an IDE but a simple text editor like notepad, etc. and to compile manually by javac.

Now to your problem: The code really works, how Jarek explained. The compiler generates from the enum the following class: <blockquote>code:
<pre name="code" class="java">class Animals extends Enum{

public static final Animals Dog;
public static final Animals Cat;
....
}
</pre>
</blockquote>So "a.Dog" just references the (static) member variable "Dog" of class Animal. Of couse "sound" must not be private in your example.
[ July 11, 2008: Message edited by: Ralph Jaus ]
 
Zheng Wangz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ralph Jaus:
Hi Zheng,

generally, while studying for SCJP, it's better not to use an IDE but a simple text editor like notepad, etc. and to compile manually by javac.

Now to your problem: The code really works, how Jarek explained. The compiler generates from the enum the following class: <blockquote><font size="1" face="Verdana, Arial">code:</font><hr><pre name="code" class="java"><font size="2">class Animals extends Enum{

public static final Animals Dog;
public static final Animals Cat;
....
}</font></pre><hr></blockquote>So "a.Dog" just references the (static) member variable "Dog" of class Animal. Of couse "sound" must not be private in your example.

[ July 11, 2008: Message edited by: Ralph Jaus ]



Hi,
Thank you for your help. I understand what you've said. and tried what you suggested. But it doesn't work.
<blockquote>code:
<pre name="code" class="java">
./tst.java:7: not a statement
a.DOG;
^
1 error
</pre>
</blockquote>
here's the code
<blockquote>code:
<pre name="code" class="java">
enum Animals{
DOG,CAT,FISH
}
public class tst {
static public Animals a;
public static void main(String[] args) {
a.DOG;

}
}
</pre>
</blockquote>
And I check the java version also : javac 1.6.0_06
And I'm really confused in the conception of this(if you're right.)
A class is used to describe the object (the comportment the capacity ...)
So a class is a meta-object.
And the static fields in a class is used to describe the class. So it's sort of meta-class.
For me, the objects cannot use these stastic fields(or methods).

[ July 11, 2008: Message edited by: Zheng ]
[ July 11, 2008: Message edited by: Zheng ]
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"a.Dog;" is the problem (how the compiler explained). What should it be ?

Use for example:

public static void main(String... args){

System.out.println(a.Dog);
Animals b = a.Dog;
}

That should work.
[ July 11, 2008: Message edited by: Ralph Jaus ]
 
Zheng Wangz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ralph Jaus:
"a.Dog;" is the problem (how the compiler explained). What should it be ?

Use for example: That will work.

[ July 11, 2008: Message edited by: Ralph Jaus ]

[ July 11, 2008: Message edited by: Ralph Jaus ]



Thank you. It works.
Now I should find out what's the meaning behind that...
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zheng - Please check your private messages again.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zheng:
Check your eclipse settings. You must have set it to display the warnings as errors (or atleast the mentioned static access). This is a standard warning which I have seen many times in my past.
Hope this helps.
Sharad
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Zeng..

Its great that you have got the new book.. can you throw some light on the differences and updates in the new scjp study guide for 310-065 over the older one scjp 310-055..

excluding the topics added for scjp 6...

Am planning to buy a book for scjp 1.5 ... should i go for scjp 1.5 or scjp 1.6 study guide...

thanks in advance..
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vipun, please do not duplicate posts. I've shared some light on your question in your original post.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Where Did you got the book for SCJP 6, who's the author how moch the price ? whats the publication??
because,i could not find a book here in india
 
Ranch Hand
Posts: 54
Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is the SCJP 6.0 study guide released in india ??
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Zheng Ren wrote:Hello everyone,
I've just bought the <SCJP study guide for java 6> and have finished the self test of chapter 1. I think there's an error for question 4 :
<blockquote>code:


<pre class="java">
enum Animals{DOG("woof"), CAT("meow", FISH("burble");....}
Class TestEnum{
static Animals a;
public static void main(...){
System.out.println(a.DOG.sound+" "+a.FISH.sound);
}
}
</pre>
</blockquote>
the answer is this code will work?! I don't think we can access a static field like this(it should be Animals.DOG no?)
[ July 11, 2008: Message edited by: Zheng ]



times like this
you should try to run the code
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ralph Jaus wrote:Hi Zheng,

generally, while studying for SCJP, it's better not to use an IDE but a simple text editor like notepad,



There's pros and cons to an IDE, I prefer a mix of both especially when it comes to complex code practice, where for me at least the automatic code completion and popup javadoc is an invaluable learning tool. However, if you are on windows, I do not recommend using Notepad, it can cause problems in regards to newlines and spaces. If you wish a simple solution I suggest Notepad2, Notepad++ or even wScite for windows, which is open source and allows you to compile and run java code in the editor.

Hope this helps
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note that this is an old thread (July / August 2008).
 
Arch enemy? I mean, I don't like you, but I don't think you qualify as "arch enemy". Here, try this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic