• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

This code is not acting the same way in NetBeans

 
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following your response and going over the section about Immutable classes in the book again, this code should be an example of immutable class with non-String parameter.
1. If not, please let me know which line(s) making it mutable.
2. If it is, since all my test print statements are producing result of 5, please let me know what is a proper test to show a class is immutable.

 
Marshal
Posts: 65457
248
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a final class with one field whose value cannot be changed. Since that field is either a primitive or an immutable reference type, that is an immutable class. You cannot test for changes; any attempts would produce a compile‑time error.
You can have fields which are mutable reference types, provided you prevent them from being changed; that entails not only prohibiting reassignments, but also using defensive copies of any object coming into your object (constructor) or going out of your object (getXXX methods and similar). There would of course be not setXXX methods.
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally......it is clicking now:)
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With a couple of modifications to the code in page 243, I entered the code in NetBeans under 3 files. It compiles and runs, but does not produce any results!

 
Saloon Keeper
Posts: 3462
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you run the main method of Chimpanzee, you get the expected result of Primate, Ape, Chimpanzee. Did you run the main method of Primate, perhaps?
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I did run the main for Chimpanzee. Matter of fact both Primate & Ape files were closed when I ran it. As usual when I tried to comment out the main for the Primate file, Java didn't like it and threw an exception (Error: Main method not found in class primate.Primate).
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found it.

In netbeans the default configuration has to be adjusted to run the file different that the main file named primate.Primate. So after changing the main class in customization window from primate.Primate to primate.Chimpanzee, it runs the main in Chimpanzee and results are correct.
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question. Not to use netbeans default configuration, is there a good way to instruct Java via programming code that it should be running Chimpanzee's main, not Primate's main?
 
Piet Souris
Saloon Keeper
Posts: 3462
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the Primate class, the Ape class and the Chimpanzee class in one file, the first and the third class both having a main method defined. If I try to run that file, NetBeans asks me which of the two main methods must be run. I do not know of any setting that somehow deals with such a sitiuation. How did you arrange these classes?
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Primate.java, Ape.java, and Chimpanzee.java. The Primate.java and Chimpanzee.java files have main() method.

How does one go about putting all three in one file?
 
Campbell Ritchie
Marshal
Posts: 65457
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can put the three into one file as long as they are in the same package. Only one package declaration allowed, at the beginning of the file. Only one of the classes can be public because it has to have the same name as the file. It is usually better, however, to have the classes in separate files. That doesn't affect main methods.
 
Enthuware Software Support
Posts: 4340
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Monhan wrote:Question. Not to use netbeans default configuration, is there a good way to instruct Java via programming code that it should be running Chimpanzee's main, not Primate's main?


Use alt+f6 to run the main of the class that is currenly open in the editor instead of the one specified in project properties.
use cntrl+shift+f5 debug.
 
Paul Anilprem
Enthuware Software Support
Posts: 4340
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, not alt+f6 but shift+f6 (to run the current class)
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool, at least this will eliminate having to play with configuration dropdown.

I just can't believe after all this time there is no code in Java that will tell the compiler to execute the main() from different file. Even something like placing a code in the first line of the project's main(), to tell the compiler to shift execution to a different file's main().
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this modified code from page 244 and 245, whether I use this.size = 4 or size = 4, the code compiles and runs with exact print results. So why use this.size?

 
author & internet detective
Posts: 39448
768
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code is showing different ways. In this example, either is fine. It matters when you have a method parameter or local variable with the same name.
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I get what you're saying (at least I think I do:)). So to test, I changed line 4 from above code to protected int size = 14;. How come the print result of size, this.size, and super.size all stayed 4?
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to mention that I also added this.size = 12 after line 25 of the above code, and the print results for super.size, this.size, and size stayed 4.
 
Campbell Ritchie
Marshal
Posts: 65457
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean like this?I got this when I tried it:-

java fish.Whale
Whale at age 5 and 12 meters long with 8 fins.
Whale at age 5 and 12 meters long with 8 fins.
Whale at age 5 and 12 meters long with 8 fins.

Please explain how you got 4; since the notion that the JVM implicitly knows that a whale isn't a fish is very far‑fetched, did you recompile the Whale class?
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great I'm glad that you had this.size = 12 after size = 4. I had it other way around.

1. Which means the instance of size and size are both pointing to the same area of memory which has been reassigned from 4  to 12 in your code or from 12 to 4 in my code. Am I correct so far?
2. If so, how come setting the size in parent class (Fish) to protected int size = 14;(as I mentioned in my ‎2‎:‎43‎:‎59‎ ‎PM response), is not making any difference in the print result of super.size?
 
Campbell Ritchie
Marshal
Posts: 65457
248
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Monhan wrote:. . . the instance of size . . .

It isn't an instance. It is an int which is a primitive. The memory doesn't point to anything; it contains 0x0000_000c which in decimal is 12.

2. If so, how come setting the size in parent class (Fish) to protected int size = 14; . . . is not making any difference . . . ?

Don't declare fields of the same name in superclasses and subclasses; that can lead to no end of confusion. But you haven't made that mistake. You have one field and one field only, in the Fish class. Because it isn't private and both classes are in the same package, that same field is inherited by Whale, even though fish aren't whales. There is one field and one field only, which is visible in both classes. So there is only one value. Remember the declaration in the superclass is executed before the subclass' constructor.
You can try declaring a size field in the subclass, and seeing what happens. You will have to do a lot of jiggery‑pokery with overridden methods before you work it out. But that sort of thing is “for entertainment purposes only,” or permissible only under the head of, “I just wanted to see what happens if …”
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow looking at it again, I wonder what I was thinking when I called it an instance!

As for superclass and subclass field behaviors, I'll stick to the straight forwards for the time being before confusing myself with what ifs:)
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question.

This is a variation of code on page 264. Of course the way getName() method is accessed in the Eel3 zap() method's print statement will cause a compiler error. On the OCA exam, such a question will expect the reason to be due to abstract conditions of Eel class or (and/or) Netbean's error -  "non-static method getName() cannot be referenced from static context"?

 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Last Question, part 2.

This is a copy from page 264's last paragraph.
"There is one exception to the rule for abstract methods and concrete classes: a concrete subclass is not required to provide an implementation for an abstract method if an intermediate abstract class provides the implementation."

Following this statement and of course after correcting Eel.getName() to getName() in the zap() method's print, I made a copy of the getName() method override from Eel3 implementation to Eel2 abstract class, and forgot to comment it out of Eel3 implementation. But it compiled and ran producing the correct print result anyway! Why?
 
Campbell Ritchie
Marshal
Posts: 65457
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you remove the class name from the zap() invocation, it ceases to be static.
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So based on your reply, the right answer for such a code on the exam would be that "non-static method getName() cannot be referenced from static context", and removing the class from it would allow it to compile.

Oh, when have a chance, I appreciate a reason for the clean compile and run for the situation in my posting under heading "Last Question, part 2".
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to code on pages 276 and 277, How does one go about accessing the methods (i.e. getNumberOfFins()) within an abstract (in this case HasFins or SharkFamily) from the main(), since they cannot be instantiated?

 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I better clarify my problem.
In particular no matter how I go about accessing the methods within SharkFamily, I have compiler errors.
I must be missing a step to implement SharkFamily like any other interface.
 
Campbell Ritchie
Marshal
Posts: 65457
248
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course an abstract class can be instantiated, but only via a concrete subclass, which may occasionally be an anonymous class.
The reason the method in line 18 won't compile is because it isn't declared default. I shall leave you to find out whether the method in line 17 is or is not an implemented method because it overrides a default method.

Formatting: you are writing on a screen, not a small sheet of paper, and there is no limit to the space you can occupy. Use blank lines between successive methods.
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it came down to implementing the method for line 17 (since it was a new abstract method) within SharkFamily implementation. Now instantiating its methods is compiler error free. ! I'll be reviewing pages 274 to 276 again.
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Is it correct to assert that a final static method can not be hidden?
2. What would be a practical reason for creating a final static method?
 
Campbell Ritchie
Marshal
Posts: 65457
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Monhan wrote:1. Is it correct to assert that a final static method can not be hidden?

How could you hide a static method? You would have to create a method with the same signature in a subclass . . .

Does that help answer your question?

2. What would be a practical reason for creating a final static method?

Same as any final method. Imagine you have a class with a static sort method in, but the class can be subclassed. Allowing that method to be hidden might permit somebody to sort differently from the original version.
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about the double entry. I was checking out the font options and although I chose normal font settings, it came out very small. So I reposted it figuring no sense hurting readers' eyes.

As for final static method cannot hidden, I wanted to make sure that I correctly understood the base rule that final prevented static methods from being created in subclasses as it does non-static ones.
 
Campbell Ritchie
Marshal
Posts: 65457
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Monhan wrote:Sorry . . .

Apologies accepted

. . . final prevent[s] static methods from being created in subclasses as it does non-static ones.

Try it; I am 99% sure that you will get a compile‑time error.

Don't say non‑static, say instance. The phrase non‑static is an abomination () which should produce more error messages than the error message it appears in. Writing non‑static in that error message has misled 1,000,000 beginners into adding the keyword static to something.
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok instance it is
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this code snippet, under what condition/policy requirement (if any) the age initialization should be

1. public static int age = 10;
2. static int age = 10;

 
Campbell Ritchie
Marshal
Posts: 65457
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Monhan wrote:. . . under what condition/policy requirement (if any) the age initialization should be . . .

None.

All fields except those used as global constants, like this one, should have private access. Please tell us under which circumstances all lemurs will be the same age, which is what you are saying if you make that field static rather than an instance field. If there is any possibility of having lemurs of different ages, then that field should be an instance field, and you should use a getXXX method to allow other code to “know” the age. If that question is from a book or website, please tell us which. Is that question complete? It looks as though there should be some additional context which you haven't told us.

I think that question merits a separate thread; it is probably a bad idea to have multiple questions in the same thread.
 
Jake Monhan
Ranch Hand
Posts: 103
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code was from the OCA Study Guide pages 279 and 280. I just posed the question in my post to see if there was any reason to change the field, age, to static from instance. Which based on your answer, I would say, if at all, it has to be a very specific set of circumstances (certainly not in this code) to warrant such an attempt.

 
We don't have time for this. We've gotta save the moon! Or check this out:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!