Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8531161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:25:13+00:00 2026-06-11T09:25:13+00:00

Still relatively new to Java and I’m wondering which is the better way to

  • 0

Still relatively new to Java and I’m wondering which is the better way to handle this. I have a class constructor that takes a few parameters, and also in this class are public getters and setters:

private String name;
private Float value;

public MySampleClass(String theName, Float theValue) {
    setName(theName);
    setValue(theValue);
}

public void setName(String n) {
    this.name = n;
}

public value setValue(Float v) {
    this.value = v;
}

I’d like to do some bounds checking on this Float. It seems like the best place to put it would be in the setter:

public value setValue(Float v) {
    if (v < 0.0f) {
        this.value = 0.0f;
    } else if (v > 1.0f) {
        this.value = 1.0f;
    }
}

This code originally had the bounds checking in the constructor and again in the setter, which seemed redundant. I changed the constructor to call the setter and put the checks in there. Does that make more sense? Or am I violating some convention of which I am completely unaware?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:25:14+00:00Added an answer on June 11, 2026 at 9:25 am

    Calling overridable methods from your constructor is a bad idea. Do something more like this:

    private String name;
    private Float value;
    
    public MySampleClass(String theName, Float theValue) {
        this.name = theName;
        setValueImpl(theValue);
    }
    
    public void setName(String n) {
        this.name = n;
    }
    
    public void setValue(Float v) {
        setValueImpl(v);
    }
    
    private void setValueImpl(Float v) {
        if (v < 0.0f) {
            this.value = 0.0f;
        } else if (v > 1.0f) {
            this.value = 1.0f;
        }
    }
    

    This gives you the validation in both places and eliminates the calls to overridable methods. See this question for more on this.

    Edit: If you plan on subclassing MySampleClass and want the validation setter available, declare it protected final instead of private.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am still relatively new to unit testing. I have written a class in
I'm relatively new to concurrency in Java (still have yet to read JCIP, but
I'm still relatively new to javascript and jQuery and was just wondering this. Suppose
I'm still relatively new to Java and Android development, so I'm still unfamiliar with
I'm relatively new to Java and I'm still trying to understand the fundamentals. I
I'm relatively new to Java, eclipse and android so this could be a completely
I feel like this is a simple question, but I am still relatively new
I'm still relatively new to Java. I've tried searching for a solution, but I
I'm still a relatively new programmer, and an issue I keep having in Java
Ok, so I am still relatively new to Java and I'm making pretty good

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.