Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , , ,

Sponsored Links



Different ways to initiate an object

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 11-03-2010
Member
 
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.
Reply With Quote
  #2  
Old 11-03-2010
Member
 
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.
Reply With Quote
  #3  
Old 11-03-2010
Member
 
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.
Reply With Quote
  #4  
Old 11-03-2010
Member
 
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.
Reply With Quote
  #5  
Old 11-03-2010
Member
 
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.
Reply With Quote
  #6  
Old 11-03-2010
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Different ways to initiate an object"
Thread Thread Starter Forum Replies Last Post
Ways to disable object snap in Word 2010 M@ndy Windows Software 2 04-12-2011 08:03 PM
Getting HP AIO Device object server register class object failed message on my system Donoho Hardware Peripherals 6 06-06-2011 02:34 AM
Scope of Object or variable in Object oriented Programming Languages Dėfrim Software Development 3 08-01-2011 06:20 AM
Granting access via security group (user object vs. computer object ikaikaokaina Active Directory 2 15-04-2010 08:31 PM
How many ways to create an object for class Shanbaag Software Development 3 25-04-2009 11:08 AM


All times are GMT +5.5. The time now is 11:54 AM.