Instantiation when filename is different from classname

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • naik
    New Member
    • Jul 2007
    • 2

    Instantiation when filename is different from classname

    hi everyone my teacher ask bout one prg is here?
    [code=java]class Outer
    {
    static int x;
    class Inner
    {
    static int y;
    public static void main(String args[])
    {
    System.out.prin t(x+" "+y);
    }
    }
    }[/code]
    if file name is Inner.java then how to call it.
    note----> it isnt call simply like anby other files of java.
    Last edited by sicarie; Jul 10 '07, 04:04 PM. Reason: Please use [code=java] and [/code] tags around your code.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    1) This is the C/C++ forum. Moving to Java, but please be careful of where you post

    2) It's bad practice to have the class name different from the file name, and I don't know if there are IDEs out there that will compile it (I know NetBeans won't, and I don't think Eclipse will, but I'm not sure), but if there are, you would have to include the name of the file, but instantiate it through the class name. One of the Java experts/mods should be able to go into more detail, or correct me on this.

    And, as weaknessforcats already requested in your other thread, please read this very carefully, we don't want to have to take action against you for not following them.
    Last edited by sicarie; Jul 10 '07, 04:08 PM. Reason: Expanding on compiling issue

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by naik
      hi everyone my teacher ask bout one prg is here?
      [code=java]class Outer
      {
      static int x;
      class Inner
      {
      static int y;
      public static void main(String args[])
      {
      System.out.prin t(x+" "+y);
      }
      }
      }[/code]
      if file name is Inner.java then how to call it.
      note----> it isnt call simply like anby other files of java.
      Did you give it a try yourself? What is the name of the compiled .class file?

      kind regards,

      Jos

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by JosAH
        ..What is the name of the compiled .class file?

        ...
        If any

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by r035198x
          If any
          Shh, you're being spoilsport again ;-)

          kind regards,

          Jos

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by JosAH
            Shh, you're being spoilsport again ;-)

            kind regards,

            Jos
            Just look at his post - that's not even 10 chars. Apparently admins play by different rules than the rest of us posters...

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by sicarie
              Just look at his post - that's not even 10 chars. Apparently admins play by different rules than the rest of us posters...
              Just add a lot of spaces between words; don't tell anyone I told you because
              otherwise Mary's neglected garden hose might get angry! (see if I care ;-)

              kind regards,

              Jos

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                See ?

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by JosAH
                  Shh, you're being spoilsport again ;-)

                  kind regards,

                  Jos
                  Oops, I thought they wouldn't read the replies anyway.

                  Comment

                  • sicarie
                    Recognized Expert Specialist
                    • Nov 2006
                    • 4677

                    #10
                    Wow, that took me way too long to figure out. And by "figure out" I mean "have to be told".

                    :(

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by r035198x
                      Oops, I thought they wouldn't read the replies anyway.
                      No need to worry: usually they don't; I was just kidding ;-)

                      kind regards,

                      Jos

                      Comment

                      • praveen2gupta
                        New Member
                        • May 2007
                        • 200

                        #12
                        It will show compilation error
                        Inner.java:6: inner classes cannot have static declarations
                        static int y;
                        ^
                        Inner.java:7: inner classes cannot have static declarations
                        public static void main(String args[])
                        ^

                        What do you wants. Tell clearly

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by praveen2gupta
                          It will show compilation error
                          Inner.java:6: inner classes cannot have static declarations
                          static int y;
                          ^
                          Inner.java:7: inner classes cannot have static declarations
                          public static void main(String args[])
                          ^

                          What do you wants. Tell clearly
                          praveen, you ought to be defenestrated.

                          Comment

                          • praveen2gupta
                            New Member
                            • May 2007
                            • 200

                            #14
                            Originally posted by naik
                            hi everyone my teacher ask bout one prg is here?
                            [code=java]class Outer
                            {
                            static int x;
                            class Inner
                            {
                            static int y;
                            public static void main(String args[])
                            {
                            System.out.prin t(x+" "+y);
                            }
                            }
                            }[/code]
                            if file name is Inner.java then how to call it.
                            note----> it isnt call simply like anby other files of java.

                            Hi

                            Inner class concepts. In the inner class we can not use staic.These are non static nested classes. So your program will show an compilation error at the use of static.

                            Comment

                            Working...