lkn2993
lkn2993

Reputation: 566

Java nested classes issue

As I have recently started programming, I was a little stuck in this area of coding.

There is a programming lesson named nested classes. But when I want to use it, it actually does not do what the homework wants. Here is an example of what I need to achieve:

public class Zoo {
    ...
    public static class monkey {
        ...
    }
}

and in the main

Zoo zoo1 = new Zoo();
...
zoo1.monkey.setage(int);
...

But there is a problem here that whenever I want to call monkey from zoo1, the debugger says that it's not possible.(Remember that I want to do this without creating an instance of monkey)

Thanks in advance

Update: I am just wondering if it's a kinda language limitation, then how the oracle itself could do that rather easily with system.out.printf?

Upvotes: 3

Views: 182

Answers (3)

Vlad
Vlad

Reputation: 18633

monkey looks static to me. They should be public instead of Public, though.

I would say that setage() is not a static method. If that's the case, and if age is a property of a monkey, than it wouldn't make sense to call it statically -- whose age would you be setting?

The problem though is that you can't seem to be able to access the static inner class through a variable of the outer class type.

So it should be Zoo.monkey instead of zoo1.monkey.

If you just want to control scoping or naming, you can use packages.

For example, you could have the following:

package com.example.application.feature;

public class MyClass {
    public void f() {
        System.out.println("Hello");
    }
}

in a source file called com/example/application/feature/MyClass.java.

Upvotes: 1

Roger Lindsjö
Roger Lindsjö

Reputation: 11543

You can not access the monkey class via an instance of Zoo, it would not actually make any sense to do that. If you want to access static methods of monkey from the main you can just use the example below

public class Zoo {

    public static void main(String[] args) {
        // Example 1
        monkey.setage(3);
        // Example 2
        Zoo.monkey.setage(3);
    }

    public static class monkey {
        private static int age;

        public static void setage(int age) {
            monkey.age = age;
        }
    }
}

But what are you actually trying to accomplish?

Upvotes: 2

t1w
t1w

Reputation: 1428

Edit: I didn't see you note "(Remember that I want to do this without creating an instance of monkey)"

sometimes before asking, searching might help you to save some time.Direct Quotion from this address: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Inner Classes

As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:

class OuterClass { ... class InnerClass { ... } }

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. The next figure illustrates this idea.

An Instance of InnerClass Exists Within an Instance of OuterClass

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

Additionally, there are two special kinds of inner classes: local classes and anonymous classes (also called anonymous inner classes). Both of these will be discussed briefly in the next section.

Upvotes: 1

Related Questions