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 6112019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:41:51+00:00 2026-05-23T14:41:51+00:00

This is a very simple example where accessing directly could be dangerous class Something{

  • 0

This is a very simple example where accessing directly could be dangerous

class Something{

      public static final int MAX=123;

      private int prop;     

      final void riskyMethod()
      {
          this.prop=800; //this is wrong
      }    

      final void safeSetProp(int x)
      {
         if(x<MAX) 
         {
            prop=x;
         }
      }

}

I know it could be done encapsulating “prop” in another class,
and defining it there as private, but that’s a lot of useless code overhead!

Is there a way to force the use of methods to access same class properties?

a keyword? any trick?

I confess I have done things like this to avoid accidental access by the same class

private int IKWIDprop;          //IKWID = I Know What I am Doing

I am sure you could know a better approach =)

Regards!

  • 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-05-23T14:41:51+00:00Added an answer on May 23, 2026 at 2:41 pm

    This is your class, come on! You always know what you are doing. If you don’t, write tests. I understand you want to force the usage of getter instead of raw field in case at some point in the future the getter will be equipped with some extra logic. But because this is your class, you are fully responsible and capable of maintaining this code.

    Bizzare Hungarian notation only clutters the code. Unit test your class to make sure nobody damages your work (including yourself). Nobody reads documentation and Javadocs, not to mention nobody will understand TIASCWIAFDPUGIage (thisIsASpecialCaseWhenIAccessFieldDirectlyPleaseUseGetterInstead_Age), including you after a while.

    BTW if you force the usage of encapsulating getter inside your class, how does the getter itself access the field? So go back to earth and be reasonable. Having a strong suite of unit tests is much more helpful, reliable and provides better documentation.

    UPDATE: Actually, there is a (sort of) clean way. You can use AspectJ, with few clever pointcuts, will catch raw private field access excluding some special cases. It can be executed at compile time to fail the build or at runtime. The pointcut would say something like: “each private field access from a method that is not annotated with @ThisIsASetter should generate compile time error. Maybe somebody more fluent with AspectJ can write this?

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

Sidebar

Related Questions

Have a look at this very simple example WPF program: <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
I have this very simple C++ class: class Tree { public: Node *head; };
Here is a very simple example. public interface IMyInterfaceProperty { } public class MyProperty
Take this very simple form for example: class SearchForm(Form): q = forms.CharField(label='search') This gets
I have this very simple example that I am using to learn structs in
Take a very simple case as an example, say I have this URL: http://www.example.com/65167.html
This seems like a very simple and a very common problem. The simplest example
This very simple code gives me tons of errors: #include <iostream> #include <string> int
Here's a very simple example of what I'm trying to get around: class Test(object):
If I have a table like this very very simple example: table { table-layout:fixed;

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.