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 3 of 3

Thread: Instantiation from within Methods

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Location
    Staffordshire, United Kingdom
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Instantiation from within Methods

    Hi, I'm a novice. I want to understand why this code will not work. This is an exercise in implementing interfaces, and I've missed something fundamental. If I invoke a method (say charAt) from another class, it fails with nullpointer exception, because mySeg cannot be instantiated from within the constructor (in my code). The only way I've tried that works is instantiating mySeg within every method. e.g Segment mySeg = new Segment(jarray,0,jlen); at the start of the method. Surely, the object mySeg will exist after the constructor when the class is instantiated with:-
    char[] testArray = {'\u0054','\u0048','\u0049','\u0053'};
    CharSeqPlus myCS = new CharSeqPlus(testArray,4);

    The problem class is:-
    public class CharSeqPlus implements CharSequence{
        char[] jarray;
        int jlen;
        Segment mySeg;
     
        /* Constructor */
        public CharSeqPlus(char[] arrayIn, int lenIn) {
        jarray = arrayIn;
        jlen = lenIn;
        Segment mySeg = new Segment(jarray,0,jlen);
        /* in the other methods, mySeg is not instatiated after  */
        /* this statement, so do a new one in each method!       */
        }
        public char charAt(int locIn){
            Segment mySeg = new Segment(jarray,0,jlen);
            return mySeg.charAt(locIn);
        }
        public int length(){
            Segment mySeg = new Segment(jarray,0,jlen);
            return mySeg.length();
        }
        public CharSequence subSequence(int stIn, int enIn){
            Segment mySeg = new Segment(jarray,0,jlen);
            return mySeg.subSequence(stIn,enIn);
     
        }
        public String toString(){
            Segment mySeg = new Segment(jarray,0,jlen);
            return mySeg.toString();
        }
        public String reversed(){
            Segment mySeg = new Segment(jarray,0,jlen);
            String revString = "";
            for(int i=jlen-1;i>=0;i--) {
                revString = revString + jarray[i];
            }
            return revString;
        }
    } //class
    Thanks for your help. It must be very fundamental!
    Last edited by copeg; March 10th, 2011 at 02:24 PM.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Instantiation from within Methods

    All variables have a certain scope. If a variable is created within a certain block of code, its scope ends when that block ends. For instance
    for ( int i = 0; i < 10; i++ ){
        int j = i;//
    }
    System.out.println(j);//j is out of scope

    There are times when the scope is defined at compile time (the above example - which won't compile) versus runtime (your example). The easy solution for you is to not create the local variable inside the constructor, but use the instance variable (named the same) that is defined in your class

  3. The Following User Says Thank You to copeg For This Useful Post:

    John Davies (March 10th, 2011)

  4. #3
    Junior Member
    Join Date
    Mar 2011
    Location
    Staffordshire, United Kingdom
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Instantiation from within Methods

    Great, thanks very much! I instantiated class field mySeg outside of the class, after instantiating the new class like this:- and deleted the superfluous instantiations:-
    CharSeqPlus myCS = new CharSeqPlus();
    myCS.mySeg = new Segment(testArray,0,4); // instantiates mySeg (once only)

    Learned something new.

Similar Threads

  1. Generics and Dynamic Instantiation Problem
    By marcosmarcos in forum Collections and Generics
    Replies: 2
    Last Post: March 4th, 2011, 01:32 PM
  2. I need help with METHODS!!!
    By Slone in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 14th, 2010, 07:33 AM
  3. 1st time using methods
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 1st, 2010, 01:37 PM
  4. methods help
    By hockey87 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 9th, 2010, 10:57 PM
  5. Re-using methods?
    By Morevan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 26th, 2010, 04:04 PM