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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:04:26+00:00 2026-05-17T21:04:26+00:00

I was wondering if it is possible, using reflection, or something similar to invoke

  • 0

I was wondering if it is possible, using reflection, or something similar to invoke a method whenever a variable is accessed in read or write mode. In a nutshell my goal is similar to the behavior of C# properties.

Example: Let’s say I have a two classes A and B…

public class A{
    public int field;

    public void foo(){ System.out.println("Field modified");}
}

public class B{
    public void bar(A a){
        a.field = 1;
    }
}

…and I want to execute the method A.foo() whenever a.field is written.

Is this achieveable in Java?

Thanks in advance

p.s. I’m aware that good programming practices suggest to use getter and setter. I just need to know if what I want to do is feasible.

ac

  • 1 1 Answer
  • 1 View
  • 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-17T21:04:27+00:00Added an answer on May 17, 2026 at 9:04 pm

    Not via reflection – reflection simply lets you get access to static information about classes/fields/etc at runtime.

    Aspect oriented programming is more what you’re thinking of – this lets you inject small bits of code (called “advice”) at certain points of your source code (such as before a method runs, when a field is set, etc.). This would almost certainly let you do what you are looking for.

    A word of warning though – as you seemed to pick up, it’s unlikely to be a good idea to rely on aspects for your general logic as it’s very hard to reason with them, and work out exactly what will happen when (by definition, they break the default mental model that calling a method foo() means that control passes directly to that method). AOP is great for things like logging or debugging though, as it lets you write small bits of code that affect a lot of cross-cutting concerns with little effort, and don’t require you to modify the code of the underlying application. Your advice can be weaved in as required, and simply left out when not required.

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

Sidebar

Related Questions

I'm wondering if the following may be possible using something like jQuery. I've looked
I'm using reflection to examine the following method declaration and am wondering if it
I upload image files using aspupload component. I was wondering if its possible to
I'm using the Twitter API (via TweetSharp) and was wondering if it's possible to
I was wondering if it's possible to have a java package setup using a
I am wondering if it is possible using annotations to persist the attributes map
I'm wondering if the following SQL update query is possible using the equivalent SubSonic
I was just wondering if this is possible using TortoiseSVN: I have a file
I am wondering if it is possible to post data without using the standard
I'm just wondering is that even possible using forms and PHP? Thanks.

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.