Results 1 to 6 of 6

Thread: Different ways to initiate an object

  1. #1
    Join Date
    Mar 2010
    Posts
    202

    Different ways to initiate an object

    Hello,
    Here is my code
    Code:
    Public void execution() {
    	 op = new op;
    }
     
    Public getOperation(){
    	return op;
    }
    It works in case I call the method executionToto2 and if execution, I have a NullPointerException. I do not, the Operation class will be declared "private" and I want to access this class by Get ... which is public. I want to know how may ways are there to initiate a object. Any help on this will be appreciated. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    356

    Re: Different ways to initiate an object

    Hello,
    I think this is called as a local variable which you have used in your code
    Code:
    op op
    mask operation attribute in your class. I think you should do some study on the basics of the java that is the core java. If you need a help on this and that to in simple way you can visit the official site of the sun and there you can find more detailed information on this and also know the api which are used in java.

  3. #3
    Join Date
    Nov 2009
    Posts
    347

    Re: Different ways to initiate an object

    Hello,
    In this case, your method returns nothing. You created an instance of a class that will be used throughout the life of the civil execution(). In this method so you created an instance of Operation that you registered in a local variable named operation (PS: your NullPointerException must come from the fact that there is a naming conflict because when compiling, it found that 2 variables for the compiler are different but have the same name). I hope this will help you.

  4. #4
    Join Date
    Nov 2009
    Posts
    583

    Re: Different ways to initiate an object

    Hello,
    Yes, I completely agree with the above post and here is a sample example to explain the same.
    Code:
    / / Declaration of class op
    private op op;
    
    Public void execution2 () (
     op = new op ();
    )
    
    Public getOperation () (
     return op;
    )
    In this case, you created a global variable called operation, it has a lifetime equivalent to the instance of the class that contains it. Here, if you do not pass through the execution function () getOperation () returns null because it is the default value that will take your global variable so that you do not instantiate.
    Last edited by SoftWore; 11-03-2010 at 12:49 PM.

  5. #5
    Join Date
    Mar 2010
    Posts
    202

    Re: Different ways to initiate an object

    Hello,
    So the best thing to do is?:
    Code:
    / / Declaration of class op
    private op op;
    
    Code:
    
    Public void execution() {
    	 op = new op;
    }
    
    Code:
    
    Public getOperation(){
    	return op;
    }
    I guess I am correct here, if I am wrong somewhere in the code, then guide me with the correct. Thanks in advance.

  6. #6
    Join Date
    Nov 2009
    Posts
    333

    Re: Different ways to initiate an object

    Hello,
    I think you can do the following
    Code:
    Public getOperation () (
    return op;
    }
    But also this
    Code:
    public op getOperation () (
     return op;
    )
    I hope this will help you and you have understood the concept behind it.

Similar Threads

  1. Ways to disable object snap in Word 2010
    By M@ndy in forum Windows Software
    Replies: 2
    Last Post: 04-12-2011, 08:03 PM
  2. how does trim initiate ssd performance
    By Bartolomeo in forum Hardware Peripherals
    Replies: 4
    Last Post: 21-06-2011, 08:35 AM
  3. Replies: 6
    Last Post: 06-06-2011, 01:34 AM
  4. Replies: 3
    Last Post: 08-01-2011, 06:20 AM
  5. How many ways to create an object for class
    By Shanbaag in forum Software Development
    Replies: 3
    Last Post: 25-04-2009, 10:08 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,154,547.85048 seconds with 16 queries