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

  • Home
  • SEARCH
  • 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 8771555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:49:05+00:00 2026-06-13T17:49:05+00:00

I am writing a communication software that will talk to lab processes in the

  • 0

I am writing a communication software that will talk to lab processes in the control department in my uni. The processes communicate over serial port and there will be a fair bit of bit checking/manipulation. I have written a helper class like the following:

public class Channel {

    public enum Kind {DIGITAL_IN, DIGITAL_OUT, ANALOG_IN, ANALOG_OUT, COUNTER_IN};

    private int resolution;
    private int number;
    private Kind kind;
    public byte[] bits;

    public Channel(Kind kind, int resolution, int number) throws Exception {
        if (resolution % 8 != 0) {
            throw new Exception("Resolution must be divisible by 8");
        }
        this.kind = kind;
        this.resolution = resolution;
        this.number = number;
        this.bits = new byte[resolution/8];
    }


    public int getResolution() {
        return resolution;
    }

    public int getNumber() {
        return number;
    }

    public Kind getKind() {
        return kind;
    }
}

My question now is if it would be considered bad practice in this case to have my byte[] declared as public?
In my LabProcessProtocol class I will access these channels bits and change them according to what I get from the process on the serial port.

I have a hunch that Java is all about being private and using getters and setters but I’m not sure. It seems so convoluted in this case.

Thanks in advance.

  • 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-13T17:49:07+00:00Added an answer on June 13, 2026 at 5:49 pm

    Well, there’s no absolute prohibition against public fields. If you feel that is the best solution, then don’t be ashamed, go ahead an do it.

    That said, stop and think about what you want to accomplish. Making everything private is not a goal in itself – the idea is to establish invariants, to make the code easier to understand and to work with.

    So consider what you want to do with the byte[] – what operations will others want to perform on it? Consider having methods for these operations, that will be easier to understand and cleaner than exposing the field as public. Also, consider what operations you do not want to allow (that would be the part that establishes invariants). For example, direct field access would allow replacing the byte[] with another one of different length – you probably want to prevent that.

    Or maybe so much happens to this byte[] that it deserves a (wrapping) class of its own? This all depends on how it is used.

    In closing, there’s no problem with starting with a simple public field. You can always refactor it later, once you have found a more appropriate solution.

    Note: “You can always refactor it later” does not apply for classes which are part of a public API (i.e. you are writing a library to be used by other, outside projects). Design of public APIs (often just called “API design”) is much harder than design of “internal” code, this is just one example. This probably does not apply in this case, I just wanted to point it out.

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

Sidebar

Related Questions

I am writing a desktop application that will have to communicate with a windows
I'm currently writing a server which multiple clients will connect to. The communication protocol
I'm writing a Windows Service for communication with a Serial Mag-stripe reader and a
I'm writing a messaging layer that should handle communication with a third party API.
I am writing a program which needs communication between processes. my code: #lang racket
I'm writing a server application that's communication with a local sql server. Each client
I'm writing an Android app that contains both UI and separate processes running. I
I am writing an application that uses OpenNETCF.IO.Serial (open source, see serial code here
I'm writing some software that automatically connects a Bluetooth device using the Windows Bluetooth
I am writing a piece of software that consists of a kernel mode driver

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.