scjp

What does redefining static methods mean in Java?

怎甘沉沦 提交于 2019-12-03 16:55:58
问题 I've been reading a section on Statics in the SCJP study guide, and it mentions the following : static methods can't be overridden, but they can be redefined What does redefining actually mean? Is it a case of having a static method that exists in both parent and child, with the same signature, however they are referenced separately by their class names? Such as : class Parent { static void doSomething(String s){}; } class Child extends Parent { static void doSomething(String s){}; }

Enum as instance variables

泄露秘密 提交于 2019-12-03 15:11:50
问题 If you have an enum such as enum Coffee { BIG, SMALL } and a class that has an instance variable like this of the enum: public class MyClass { private Coffee coffee; // Constructor etc. } Why is it possible in the constructor to say e.g. coffee.BIG ? I don't understand that you can use the reference? Is enum as instance variables initialized to something other than null ? It is the self test question #4 in the SCJP book in the first chapter. I tried to shorten the code and question. 回答1: In

What does redefining static methods mean in Java?

断了今生、忘了曾经 提交于 2019-12-03 05:47:21
I've been reading a section on Statics in the SCJP study guide, and it mentions the following : static methods can't be overridden, but they can be redefined What does redefining actually mean? Is it a case of having a static method that exists in both parent and child, with the same signature, however they are referenced separately by their class names? Such as : class Parent { static void doSomething(String s){}; } class Child extends Parent { static void doSomething(String s){}; } Referenced as : Parent.doSomething(); and Child.doSomething(); ? Also, does the same apply for static variables

How long should I prepare for SCJP? [closed]

爱⌒轻易说出口 提交于 2019-12-03 05:13:47
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 7 years ago . Ok I know this might sound a bit silly, as it depends on the person, but on average how much time should I spend on preparing for SCJP? I already have some experience with Java (a few small and medium projects for my university, from implementing a simple "ships" game to some client-server stuff). I was thinking

Enum as instance variables

£可爱£侵袭症+ 提交于 2019-12-03 04:54:46
If you have an enum such as enum Coffee { BIG, SMALL } and a class that has an instance variable like this of the enum: public class MyClass { private Coffee coffee; // Constructor etc. } Why is it possible in the constructor to say e.g. coffee.BIG ? I don't understand that you can use the reference? Is enum as instance variables initialized to something other than null ? It is the self test question #4 in the SCJP book in the first chapter. I tried to shorten the code and question. In enum Coffee { BIG, SMALL } BIG or SMALL are public static final fields of Coffee class, and like all static

How long should I prepare for SCJP? [closed]

邮差的信 提交于 2019-12-02 17:38:33
Ok I know this might sound a bit silly, as it depends on the person, but on average how much time should I spend on preparing for SCJP? I already have some experience with Java (a few small and medium projects for my university, from implementing a simple "ships" game to some client-server stuff). I was thinking about taking it around jun-july '10 so I'd have around 7-8months, is it enough/too much/not enough? Also I've been looking for a good book, is there anything better than: "Sun Certified Programmer for Java 6 Study Guide" by Kathy Sierra and Bert Bates? And what about some practical

SCJP - override method with exception handling raises a compiler error

我只是一个虾纸丫 提交于 2019-12-02 06:19:09
In the SCJP book by Kathey Sierra, an excerpt is as follows: If a method is overridden but you use a polymorphic (supertype) reference to refer to the subtype object with the overriding method, the compiler assumes you’re calling the supertype version of the method. If the supertype version declares a checked exception, but the overriding subtype method does not, the compiler still thinks you are calling a method that declares an exception (more in Chapter 5). Let’s take a look at an example: class Animal { public void eat() throws Exception { // throws an Exception } } class Dog2 extends

Post and Pre increment operators

随声附和 提交于 2019-12-01 16:14:10
When i run the following example i get the output 0,2,1 class ZiggyTest2{ static int f1(int i) { System.out.print(i + ","); return 0; } public static void main(String[] args) { int i = 0; int j = 0; j = i++; //After this statement j=0 i=1 j = j + f1(j); //After this statement j=0 i=1 i = i++ + f1(i); //i++ means i is now 2. The call f1(2) prints 2 but returns 0 so i=2 and j=0 System.out.println(i); //prints 2? } } I don't understand why the output is 0,2,1 and not 0,2,2 i = i++ + f1(i); i++ means i is now 2 . The call f1(i) prints 2 but returns 0 so i=2 and j=0 before this i = 1 , now imagine

How to programmatically test if assertions are enabled?

隐身守侯 提交于 2019-12-01 02:37:13
One of the correct answers from OCP Java SE 6 Programmer Practice Exams is: You can programmatically test wheather assertions have been enabled without throwing an AssertionError . How can I do that? I use this boolean assertOn = false; // *assigns* true if assertions are on. assert assertOn = true; I am not sure this is the "official" way. I guess you should use Class.desiredAssertionStatus() http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#desiredAssertionStatus() Joe The Oracle Java Tutorial provides information about how to do it... http://docs.oracle.com/javase/7/docs

How does Java handle String objects in memory?

一笑奈何 提交于 2019-11-30 20:53:10
I was asked this question: String s = "abc"; // creates one String object and one // reference variable In this simple case, "abc" will go in the pool and s will refer to it. String s = new String("abc"); // creates two objects, // and one reference variable* Based on above details how many String objects and how many reference variables were created prior to the println statement of below code? String s1 = "spring "; String s2 = s1 + "summer "; s1.concat("fall "); s2.concat(s1); s1 += "winter "; System.out.println(s1 + " " + s2); My answer was The result of this code fragment is spring winter