简体   繁体   中英

Java how to access a class within a class

Here is basically what I have:

public class Game extends Activity{
    public class Work{
        public class Shuffle{
            *Class I need to access*
        }
    }
}

Here is the class I will be accessing Shuffle from:

public class Deck extends Game {
    public int shuffle() {
        //WHAT DO I NEED TO DECLARE HERE IN ORDER TO ACCESS Shuffle.getShuffle()?
        int[] shuffDeck = (MY SHUFFLE CLASS).getShuffle();
        int x = shuffDeck[i];
        String y = String.valueOf(x);
        i += 1;

        return x;
    }
}

What do I need to declare to be able to access Shuffle.getShuffle() in my Deck class?

Now taking of Nested Classes its of 2 types :

1. Inner Classes (Non-static)

2. Top Level Classes (static)

- Inner Clas s (Non-static) has an Implicit Reference to its Outer Class (Enclosing Class). To access an Inner Class from outside you need to access it using the Outer Class Object .

Eg:

 public class Outer{


     class Inner{


     }

 }


public class Test{


   public static void main(String[] args){

         Outer o = new Outer();
         Outer.Inner i = o.new Inner();

    }
} 

- A Top-Level Class (static) is just like a separate class in the Outer Class. Top-level class needs to create an object of the Outer Class to access its Non-static members , but It can Access the Static methods and Static variables of the Outer class directly.

Eg:

public class Outer{


         class Inner{


         }

     }


    public class Test{


       public static void main(String[] args){


             Outer.Inner i = new Outer.Inner();

        }
    } 

You cant access Inner class methods, and fields, from outer classes, directly. However you can access outer class methods, inside inner classes.

To access inner classes, you need to create an object of inner class, than only you can access inner class fields. See nested classes for more clarifications:

http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

You are talking about nested classes, try to read also this article. Maybe it can help.

http://en.wikibooks.org/wiki/Java_Programming/Nested_Classes

A nested class should exist only to server outer class. You should not expose inner classes to outside world.

public class Game extends Activity{


public static class Shuffle
        {
            // provide shuffle method here which will use internal implementation of
            // shuffle.
        }

int[] shuffle()
    {
        // call inner class method from here. Also declare inner class as
        // static since I guess your inner class does not require instance
        // of outer class.
       return null;
    }

And access shuffle() method of Game using Object of Game

   int[] shuffDeck = (Game object).getShuffle();
   public class Outer{
        class Inner{

       }
    }

    Outer.Inner art = (new Outer()).new Inner();

import android.content.Context;
import android.widget.Toast;

public class Outer{
    private Context context;
    public Outer(Context con) {
        context = con;
        String text = "Hello, I'm dbManager.";
        int duration = Toast.LENGTH_SHORT;
        Toast.makeText(context, text, duration ).show();
    }

    public class Inner{
        public Inner() {
            String text = "Hello, I'm «Art dbManager».";
            int duration = Toast.LENGTH_SHORT;
            Toast.makeText(context, text, duration ).show();
        }
    }
}

    Outer.Inner art = (new Outer(this)).new Inner();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM