Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Calling a nested class from a different java source file?

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Calling a nested class from a different java source file?

    I am pretty new to java, and I'm trying to write a simple program for practice, but it requires me to call a nested class from a different java source file, and I'm not sure how to do that... I don't think I need to post my code, but if I do need to, don't be afraid to ask.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,620
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Calling a nested class from a different java source file?

    I'm not sure what a nested class is. Could you post a small program that shows your problem?

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calling a nested class from a different java source file?

    Sure.

    public class example{
           public static void main(String[] args){
                   public class nestedClass{ /*This is what I meant*/
    Last edited by Pyrius; July 19th, 2011 at 03:57 PM. Reason: Forgot to add [/highlight] at the end of the code.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,620
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Calling a nested class from a different java source file?

    Does that compile? You show a class defined inside of a method

    Have you looked in the Tutorial for nested classes?
    http://download.oracle.com/javase/tu.../java/TOC.html
    Last edited by Norm; July 19th, 2011 at 04:02 PM.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Pyrius (July 19th, 2011)

  6. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calling a nested class from a different java source file?

    Oh! Thanks! I read a bit, then found this syntax: OuterClass.InnerClass innerObject = outerObject.new InnerClass();

  7. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Calling a nested class from a different java source file?

    Yup - the access specifiers (public, protected, private) work the same as for variables, but the OuterClass.InnerClass syntax is a bit messy - which is, perhaps, a hint that it's not expected to be a commonly used construct. More usually, the OuterClass is given a method that will return a new instance of the inner class (which can then be private or anonymous). An example of this is the iterator() method provided by List objects (e.g. ArrayList), that returns an instance of an inner Iterator class.

  8. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Calling a nested class from a different java source file?

    My personal beliefs are that if you have a nested/inner class then the purpose of that class is to be used by (and only by) the enclosing class. If the nested/inner class DOES need to used/accessed by another class then why the hell is it a nested/inner class in the first place? It should be a top level class in it's own right.
    Improving the world one idiot at a time!

  9. #8
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Calling a nested class from a different java source file?

    Quote Originally Posted by Junky View Post
    My personal beliefs are that if you have a nested/inner class then the purpose of that class is to be used by (and only by) the enclosing class. If the nested/inner class DOES need to used/accessed by another class then why the hell is it a nested/inner class in the first place? It should be a top level class in it's own right.
    True in general, but there are situations (e.g. anonymous Iterators, factory classes, etc.), where it just makes more sense to use an inner class that returns an interface implementation. That way, the implementation is kept private. With things like anonymous Iterators, it's a way of extending the API of the host/owner class without explicitly adding a whole bunch of methods to it.

Similar Threads

  1. Calling method from .class file
    By alexx_88 in forum Java Theory & Questions
    Replies: 6
    Last Post: April 24th, 2012, 02:14 AM
  2. about calling sub class
    By pokuri in forum Object Oriented Programming
    Replies: 3
    Last Post: January 11th, 2011, 02:30 PM
  3. calling a java a class connected to derby from a batch file
    By Reem in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 31st, 2010, 03:01 PM
  4. Help Calling Method From Another Class
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2010, 09:24 AM
  5. [SOLVED] How to call string in another class in java?
    By tazjaime in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2009, 09:31 AM

Tags for this Thread