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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:20:03+00:00 2026-05-24T20:20:03+00:00

I’ve been looking into Java reflections. This is an example from Wikipedia http://en.wikipedia.org/wiki/Reflection_(computer_programming ):

  • 0

I’ve been looking into Java reflections. This is an example from Wikipedia http://en.wikipedia.org/wiki/Reflection_(computer_programming):

// Without reflection
new Foo().hello();

// With reflection
Class<?> cls = Class.forName("Foo");
cls.getMethod("hello").invoke(cls.newInstance());

I find this a bit counter-intuitive because the method is called before creating an instance.

Reflections of course could be useful to call game leves, especially if there are hundreds of them.

EDIT – SOME SOLUTIONS:

Here is a simple example of reflection which works on Android:

    try {                
        Class<?> clazz = Class.forName("com.test.Foo");                
        clazz.newInstance();            
    } catch (Exception e) {                
        throw new IllegalStateException(e);            
    }

and the class file

public class Foo {

    Foo(){  
        hello();    
    }

    private void hello() {  
        Log.e("FOO", "Hello!");
    }
}

Suppose one wants to call an Activity by reflection:

Activity activity;
try {                
  Class<?> clazz = Class.forName("com.test.MyLevelActivity");                
  activity = (Activity) clazz.newInstance();            
} catch (Exception e) {                
  throw new IllegalStateException(e);            
}
startActivity(new Intent(this,activity.getClass()));

Suppose a level which contains data and methods should be ‘loaded’ by reflection:

    Level currentLevel;
    try {                
        Class<?> clazz = Class.forName("com.test.Level_1_1");                
        currentLevel = (Level) clazz.newInstance();            
    } catch (Exception e) {                
        throw new IllegalStateException(e);            
    }

    String levelName = currentLevel.getLevelName();
    String result = Integer.toString(currentLevel.processData(3, 7));

    Toast.makeText(this, levelName + " result= " + result, Toast.LENGTH_LONG).show();

Here are the classes:

public abstract class Level {

   public abstract String getLevelName();

   public abstract int processData(int a, int b);

}


public class Level_1_1 extends Level{
   private String levelName = "level 1.1";

   public String getLevelName(){
      return levelName;
   }

   public int processData(int a, int b) {
      return a * b;
  }
} 
  • 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-24T20:20:05+00:00Added an answer on May 24, 2026 at 8:20 pm

    here

    foo.hello();
    

    can’t work, foo is just an object that does not have a method hello().

    Things that are unfamiliar may seem counter-intuitive, but eventually new idioms become natural. Just go with the standard approach.

    To understand it, consider that the method definition is not part of the object, you write the method once for the class, it “lives” independently of any given object. Hence it’s quite reasonable for the class to say “hey method, apply yourself in the context of this object, he’s one of us”

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have a French site that I want to parse, but am running into
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.