Home Get only properties count for java class
Reply: 1

Get only properties count for java class

tkrz
1#
tkrz Published in 2017-12-07 11:16:10Z

Ok, so i have tried to get count of properties for class. I have seen two related questions, but they are not quite answering this.

question one

question two

problem with those two is, that

SomeClass.class.getDeclaredFields().length

and

SomeClass.class.getDeclaredMethods().length

are not quite return what i would expect them to. I got sample class:

SomeClass {
    public static final int ONE = 1, TWO = 2, THREE = 3;

    public static int someMethodOne(){}
    public static int someMethodTwo(){}
}

Declared fields return 5 which according to this is wrong, and should be 3. Declared methods does not help either, because it returns 3, while there are only 2 declared methods. What is wrong with this?

Edit: trying to do this with Java 8 & Android Studio

Stefan
2#
Stefan Reply to 2017-12-07 12:25:06Z

I get 3 and 2 as expected when I run the code below. If you get other values, you could also print the names of the fields and the methods (see below).

public class SomeClassChecker {

    public static void main(String[] args) {
        System.out.println(SomeClass.class.getDeclaredFields().length);
        System.out.println(SomeClass.class.getDeclaredMethods().length);

        for (Field field : SomeClass.class.getDeclaredFields()) {
            System.out.println("field: " + field.getName());
        }

        for (Method method : SomeClass.class.getDeclaredMethods()) {
            System.out.println("method: " + method.getName());
        }

    }

} 

Java 8 & Eclipse. I get this (expected) output:

3
2
field: ONE
field: TWO
field: THREE
method: someMethodOne
method: someMethodTwo
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308142 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO