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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:05:01+00:00 2026-06-11T14:05:01+00:00

I am currently experimenting with reflection and I recently came across a question. I

  • 0

I am currently experimenting with reflection and I recently came across a question. I have this method, which calculates the size (unit: bytes) of a class consisting only of primitive typed fields (excluding booleans) as follows:

import java.lang.reflect.Field;

public class Foo {

    // [...]   

    // Works only for classes with primitive typed fields (excluding booleans)
    public int getSize() {
        int size = 0;

        for (Field f : getClass().getDeclaredFields()) {
            try {
                f.setAccessible(true);
                Object obj = f.get(this);
                Field objField = obj.getClass().getDeclaredField("SIZE");
                int fieldSize = (int) objField.get(obj);
                size += fieldSize / Byte.SIZE;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return size;
    }

}

As you can see the method cannot be static, since it contains non-static stuff like getClass() and this. However, for every instance of this class the return value of getSize() will be the same, which also holds for every class which extends Foo (of course, with a different value). Hence, conceptually getSize() has a static nature.

Is there a way to make this method static? I thought of using a static class reference like Foo.class to get around getClass() but this would basically destroy the semantics of getSize() for classes which extend Foo. I currently do not think it is possible since static methods are not inherited, but I am not 100% sure if there might be a little tweak to handle this issue.

  • 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-11T14:05:02+00:00Added an answer on June 11, 2026 at 2:05 pm

    So what’s wrong with doing something like

    public static int getSize(Object parent) {
        int size = 0;
    
        for (Field f : parent.getClass().getDeclaredFields()) {
            try {
                f.setAccessible(true);
                Object obj = f.get(parent);
                Field objField = obj.getClass().getDeclaredField("SIZE");
                int fieldSize = (Integer) objField.get(obj);
                size += fieldSize / Byte.SIZE;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        return size;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently experimenting with various input controls and have stumbled upon SneakyInput, which
I have a question regarding mysql memory settings. I am currently experimenting on very
I'm currently experimenting with the Yii framework and have come across a slight snag.
Im new to objC and Im currently experimenting on UISearchDisplayController. Basically I have an
Hi I'm currently experimenting with Raphael JS 2 and have produced a nice circle
I'm currently experimenting with the WPF NHibernate toolkit and I have to say: it's
I am currently experimenting with TFS, and I really like it. One small question,
I'm currently experimenting with extension methods. My goal is a extension method for List
I'm pretty new to unit testing and currently experimenting a bit with Visual Studio's
Currently I'm experimenting with this code (I know it doesn't fit the purpose). I

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.