1 2 Previous Next 17 Replies Latest reply on Apr 30, 2009 7:31 PM by 843789

    passing object references via methods

    843789
      Why is the JFrame object not being created in the following code:
      public class Main {
        public static void main(String[] args) {
          Main main = new Main();
          JFrame frame = null;
          main.createFrame(frame);  // <-- does not create the "frame" object
          frame.pack();
          frame.setVisible(true);
        }
          
        private void createFrame(JFrame frame) {
          frame = new JFrame("createFrame");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
      }
      Thanks.
        • 1. Re: passing object references via methods
          mlk
          Read:
          [http://www.javaranch.com/campfire/StoryCups.jsp]
          and
          [http://www.javaranch.com/campfire/StoryPassBy.jsp]
          • 2. Re: passing object references via methods
            843789
            It is being created, local to you method. And since the reference is passed by value (a copy is made) the reference goes back to its previous value (null) upon return from the method.
            • 3. Re: passing object references via methods
              843789
              Because Java uses call-by-value. Your code, fixed:
              import javax.swing.*;
              
              public class Main {
                  public static void main(String[] args) {
                      Main main = new Main();
                      JFrame frame = main.createFrame();
                      frame.pack();
                      frame.setVisible(true);
                  }
              
                  private JFrame createFrame() {
                      JFrame frame = new JFrame("createFrame");
                      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      return frame;
                  }
              }
              • 4. Re: passing object references via methods
                baftos
                Your code is absolutely identical with:
                public class Main {
                public static void main(String[] args) {
                Main main = new Main();
                JFrame frame = null;
                main.createFrame(frame);  // <-- does not create the "frame" object
                frame.pack();
                frame.setVisible(true);
                }
                
                private void createFrame(JFrame zzz) {
                zzz = new JFrame("createFrame");
                zzz.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                }
                }
                The coincidence of names in main() and in createFrame() does not mean that the variable in main() will be changed.
                Java is not pass-by-reference, if this is what you expected. Change the code to:
                public class Main {
                public static void main(String[] args) {
                Main main = new Main();
                JFrame frame = main.createFrame();
                frame.pack();
                frame.setVisible(true);
                }
                
                private JFrame createFrame() {
                JFrame zzz = new JFrame("createFrame");
                zzz.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                return zzz;
                }
                }
                This will work and if you don't understand why, post here your concerns.
                • 5. Re: passing object references via methods
                  843789
                  These explanations are great; real eye openers.
                  OK. This could be a "way out in left field" question. I am just starting out with Java. But I want to ask:

                  Objects are stored in the heap?
                  Object references and primitives are stored on the stack?


                  Adjusting heap size is straight-forward. A larger heap can store more/larger objects. What about stack sizes?

                  C:\Dev\java -Xss1024k Main

                  I assume this refers to method's stacks. But, what about object scoped, and class scoped, object references?
                  public class Main {
                    private static List list = new ArrayList(); // class scoped
                    private JFrame frame = new JFrame();  // object scoped
                  
                    public static void main(String[] args) { ...... }
                    private void createFrame() { .... }
                  }
                  How is the reference to list and frame stored, with regard to memory management?
                  Do objects have stacks?
                  Do classes have stacks?
                  If not, how are the list and frame references stored (which helps me understand reference scoping).
                  Would the overflow of a method stack (ex. via recurssion) also overflow the method's object and the method's class stacks?

                  If these questions are stupid, "out of left field", don't matter, etc. Just ignore them.
                  But the knowledge could help me avoid future memory related mistakes, and maybe pass a "Java Certified Developer" exam?

                  My original question is already answered, so thanks to all.
                  • 6. Re: passing object references via methods
                    843789
                    I don't want to sound like I'm going to smack your knuckles with a ruler, but if you're just starting out in Java, don't attempt to write GUI code. Learn how to program, first.
                    • 7. Re: passing object references via methods
                      796447
                      rikasai wrote:
                      Objects are stored in the heap?
                      Object references and primitives are stored on the stack?
                      Adjusting heap size is straight-forward. A larger heap can store more/larger objects. What about stack sizes?
                      How is the reference to list and frame stored, with regard to memory management?
                      Do objects have stacks?
                      Do classes have stacks?
                      If not, how are the list and frame references stored (which helps me understand reference scoping).
                      Would the overflow of a method stack (ex. via recurssion) also overflow the method's object and the method's class stacks?

                      If these questions are stupid, "out of left field", don't matter, etc. Just ignore them.
                      Actually it may just be my own take on this, but this strikes me as you asking a bunch of homework questions, as if to cheat.
                      • 8. Re: passing object references via methods
                        843789
                        warnerja wrote:
                        rikasai wrote:
                        Objects are stored in the heap?
                        Object references and primitives are stored on the stack?
                        Adjusting heap size is straight-forward. A larger heap can store more/larger objects. What about stack sizes?
                        How is the reference to list and frame stored, with regard to memory management?
                        Do objects have stacks?
                        Do classes have stacks?
                        If not, how are the list and frame references stored (which helps me understand reference scoping).
                        Would the overflow of a method stack (ex. via recurssion) also overflow the method's object and the method's class stacks?

                        If these questions are stupid, "out of left field", don't matter, etc. Just ignore them.
                        Actually it may just be my own take on this, but this strikes me as you asking a bunch of homework questions, as if to cheat.
                        You got me all wrong. Not been in school since 2003, and my degree had nothing to do with software.
                        Since the guys who developed Java help so much on these forums, why would anyone pay a professor to teach them Java?
                        • 9. Re: passing object references via methods
                          796447
                          rikasai wrote:
                          You got me all wrong. Not been in school since 2003, and my degree had nothing to do with software.
                          If you say so.
                          Since the guys who developed Java help so much on these forums, why would anyone pay a professor to teach them Java?
                          Well if everyone just came here to have people on the forum teach them everything, I doubt there would be very much helping going on.
                          In fact this appears to happen all the time, and we generally just shoot them down, deferring them back to their classrooms. Too much work to hold everyone's hand on the forum.
                          • 10. Re: passing object references via methods
                            843789
                            rikasai wrote:
                            Since the guys who developed Java help so much on these forums, why would anyone pay a professor to teach them Java?
                            I've only rarely seen a Sun employee on this forum actually as it's mostly made up of volunteers. Regarding the second portion, most programming is learned on your own. My experience here has been that the quality of answers received usually depends directly on the quality of the questions asked, and I know that my questions (and resultant answers) have improved greatly since reading [How To Ask Questions The Smart Way|http://catb.org/~esr/faqs/smart-questions.html]

                            Of course, YMMV.

                            best of luck
                            • 11. Re: passing object references via methods
                              843789
                              rikasai wrote:
                              Since the guys who developed Java help so much on these forums, why would anyone pay a professor to teach them Java?
                              This statement reflects a very common misconception: learning Java is learning to program.

                              It is like saying that you learn to design and build a house by learning how a hammer, screwdriver, pliers, pipe wrench, solder equipment, and other tools work. Java is but an means whereby you implement your idea... the programming should come long before you ever touch that source editor or compiler.

                              Learning to program is why people pay Teachers, go to college, and rather inconsequently they may learn a particular language along the way.
                              • 12. Re: passing object references via methods
                                843789
                                I sure don't understand why I am being run out of here:

                                (1) my subject line was very specific. (ie. not "help with project", or "what should i do?", or "what an I missing?")
                                (2) I posted code that compiles.
                                (3) I made a begginer mistake and this is "New to Java" forum.
                                (4) I said my first question was answered, and complimented the guys who helped by calling them "the guys who wrote Java".
                                (5) I said that if my second question was stupid, then just ignore it.
                                (6) I was called a cheater, and took it politely.

                                I made a very specific question about how Java manages memory as it relates to object scoping.
                                That seems like a really good, well worded, question to me.
                                I really don't understand.

                                And I am teaching myself Java. And I get stuck sometimes.

                                Edited by: rikasai on Apr 30, 2009 6:12 PM
                                • 13. Re: passing object references via methods
                                  843789
                                  rikasai wrote:
                                  I sure don't understand why I am being run out of here:
                                  I just read the thread and don't see why you think you're being "run out of here". Could you point it out to me?

                                  Also note that while this forum can be very helpful for answering specific questions, it is in no way a substitute for a good tutorial/book/teacher/tutor.

                                  You will need to do some learning before you can ask the correct questions.

                                  For example your questions about stack/heap show that you'll have to do some learning about what a stack is and how it's used in programming languages generally (that topic isn't Java-specific, by the way). Try [this Wikipedia article for example|http://en.wikipedia.org/wiki/Stack-based_memory_allocation].
                                  • 14. Re: passing object references via methods
                                    843789
                                    rikasai wrote:
                                    I sure don't understand why I am being run out of here:
                                    No one's running you anywhere. We're expressing opinions, which is the purpose of an internet (or any) fora. You may agree with them or disagree, but either's OK.
                                    1 2 Previous Next