Win a copy of Head First Go this week in the Go forum!

Campbell Ritchie

+ Follow
since Oct 13, 2005
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Campbell Ritchie

You can't set anything via the instances. A static method cannot access instance data, so any information required by the method has to be passed as an argument:- Color c = JColorChooser.showDialog(myPanel, "Colour Please:-", "Color.SKY_BLUE_PINK); Beware: that method can return null. A colour chooser is different from a file chooser where you usually create an instance first.
7 hours ago

Fred Masen wrote:. . . Starting out with Java . . . Tony Gaddis

When does that book introduce objects?

. . . Head First  Java and Head First OOP. . . .

Don't buy new copies if you can get a decent copy second‑hand for half the price.
7 hours ago
Welcome to the Ranch

How do you know that OP didn't download the JDK? It is more common to see that error message because you have installed the right thing but not set the system PATH correctly. See our FAQ. If you use Windows®10 and you get the PATH elements in a dropdown list in ttheir own window, make sure to give each PATH element a line to itself.
Also avoid <= if you can. There is a basic format for a for loop, which isAlways start from that basc form, even if you have to change it because you are iterating things slightly differently. If you write for (int i = 0; i < 6; i++) ... everybody will look at that for 1″ and see a loop running 6×, which they won't if you use <=. It will be easier to readthan to use <=.
7 hours ago
Look at the whole of the method summary. Underneath where you looked it says,

Methods inherited from interface java.util.Collection

where there is a second remove() method, taking Object as its parameter.
The main method isn't a function; a function always returns something.
A null is rather like a wasp which would like to share your ice cream while you are wearing only a swimsuit. You have several options:-
  • 1: Put your glass over the wasp and trap it where it can't get at you to do you any harm. That is what you do with Lists and Maps where you can hide the nulls where they won't cause any trouble.
  • 2: Swat it with the newspaper. Make sure neither wasps nor nulls come anywhere near you, but you have to be careful not to miss or you will simplly annoy it. Make sure all fields are initialised to “real” values by the time the constructor completes. Don't allow nulls into any of your methods. Use the standard techniques (as shown in Effective Java by Joshua Bloch) to avoid returning null.
  • 3: Always know where it is. That is what you have to do when implementing a linked list or a binary tree. You know there will be nulls somewhere: make sure you know where they are and take precautions to stop them causing trouble. That is where x != null tests and Optional<T> objects come in useful.
  • 4: This is where nulls are nastier than real wasps. Just ignore it and let it find somewhere where it really hurts to sting you. Trying to throw a null rather than a real Exception or unbox null rather than a real Integer come into the same category.
  • 7 hours ago
    If it is a static method, it ignores all instance fields and value and data (it has to, since a static method can never access such values.
    Have you been through the Java™ Tutorials?
    10 hours ago
    You are probably better off deleting chooser altogether and using the static method properly: JColourChooser.showXXXDialog(...); as Junilu told you earlier.
    I can also see other things I don't like. I don't think your application IS‑A JFrame, but is does HAVE‑A JFrame and it does USE‑A JFrame. So I think there are very few places where it is actually good design to extend a display class. I also don't think a frame should implement action listener or similar. Make a separate action listener class λ and add that to the appropriate button etc.
    11 hours ago
    GR, please search for books, but I think it is too early for anybody to have written anything yet.
    Java9 introduced methods like this, this and this. Note they are all heavily overloaded.
    19 hours ago
    You know your method to get an average is mathematically brittle? If you try to get the average of 4 and 6, it will give the correct answer (5). If you apply that answer to 9 and try for another average, you will get the result 7, though the correct average (arithmetical mean) of 4 6 9 is 6⅓. That is because averaging is not an associative operation; you can only apply that technique once.
    20 hours ago
    Welcome to the Ranch

    If you had indented your code correctly, and used the code button, your code would have looked like this:-. . . and that would make it obvious what Carey said about incorrectly placed {}. I can see some other places where you are mixing types which will fail to compile.
    Find the old Sun Style Guide, whch tells you not to writeIt says to writeWhy are you returning a String from you method rather than a boolean?
    What has upper case writing got to do with ISBNs?
    20 hours ago
    Moved to our Eclipse forum.

    I have in the past found that Eclipse lags behind the most recent Java® versions.
    20 hours ago
    . . . Welcome (again)
    1 day ago
    Agree; you cannot predictt which questions you will do well on, so you are taking a great risk if you sit the exam now.

    And welcome to the Ranch