Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Carey Brown
  • Frits Walraven

How Can i Call the Innerclass in main Method ? is that possible ?

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Innerclass {
   public static void main(String[] args) {

       Innerclass obj = new Innerclass();


class Outerclass {

   class Innerclass {

       void displayMessege() {
           System.out.println("Welcome to the Test...");


Ranch Foreman
Posts: 95
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you want to post code the editor helps you with the CODE button - as you're new here let me help you with this:

As you're new here you're may not familiar with how to use this forum. Hence I'd like to point out, that it may not be the way to just post a few lines of codes without any information or question. Just using the threads title is not the best way.

Anyway, as the subject was long enough to fit your whole question, let me answer it: Basically yes, it is possible, but not with the code you posted.
You see, as classes in Java has to have unique names having more than one class with the same name is never a good idea. You can get along with it by using different packages, but when you look at java.util.Date vs. java.sql.Date and you want to use both within the same code you encounter the inconvenience to always have to specify the full classname including the package, which can make code sometimes hard to read. Also: Using the keyword "class" can lead to issues any hence should be avoided (as any reserved keywords) So, first of all, let's rename your classes to help to clarify some things up a bit:

The next issue you encounter with this example is access: Sure, there is a way to get the code print the message to the output - but it requires some modification (there're several ways to do it, but I stick to the very basic ones here). To work this out you first has to figure out which class knows what other class and which class has what access to what other class.
In your given example the class Main only has access to the class Outer, but not directly to the class Inner. Why is that so? Well, first of: The class Inner is missing the static keyword. An inner class without the static modifier always requires an instance of the outer class, just like any other member, there's no way around that. This now comes down to the cocept of the nested classes: Does the inner class make any sense without an instance of the outer class? Why is it an inner class in the first place? What's it used for and what does it hide?
An example would be the inner interface Map.Entry: It represents one Entry in a Map - so it's easy: No, an entry in some map doesn't make sense without the map itself. Hence the inner interface Map.Entry does not have the keyword static, it can't be used without a Map. You want to have a Map.Entry? Well, then you first need a Map.
The exact opposite of that would be an inner builder class. I don'T know if you'Re already familiar with that pattern, but let me give you a very brief example:

In this case having something to plan and build your house does make sense without the House itself - hence the inner class House.Builder is static: it can be used without an instance of the outer House class. To build a house one may do this:
House house=(new House.Builder()).addFloor().addRoom().addRoom().addRoom().addFloor().colorADino().build();
This way one would end up with a house having 3 floors, 4 rooms and it be painted rainbow.

To come back to your code: To figure out a way how to modify the code so you can get the text printed you first have to figure out if the inner class can be static or not as this decides what modifications can be done. A valid way would be this: The Inner class can't be alone but always require the Outer class, then a valid solution may look like this:

Your main class would then become this:

This is just one example - if you want to get more information please try to specify what it is that you want to achieve. From there one can figure out what style may fit better than others.
Posts: 68899
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bob Winter wrote:. . . the CODE button . . . let me help you . . .

. . . . An inner class without the static modifier always requires an instance of the outer class. . .

See, for example, Thinking in Java by Bruce Eckel (4th edition pages 345‑388) and Effective Java by Joshua Bloch (2nd edition pages 106‑108, 3rd edition pages 114‑116). A public inner class is awkward to use and a public static nested class is easy to use.

. . . Why is it an inner class in the first place?

I hope this is an exercise just to see what inner classes are. Not a paradigm for production code.
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
    Bookmark Topic Watch Topic
  • New Topic