unclear new Object declaration

  • Thread starter ACompetitorsChallenge
  • Start date
A

ACompetitorsChallenge

I am working from preexisting code by a programmer who is not
available for clarification on a line of code ...


l1:import model.Manager;
l2:public class DataSourceConnectionObject {
l3: public static void main(String[] args) {
l4: try {
l5: Manager manager =null;

....

is all that line 5 is declaring is a new instance of "Manager" and
naming it "manager" and assigning the expression a "null" value
without the "new" clause used to issue it?

any help would be nice
thx
 
K

Knute Johnson

I am working from preexisting code by a programmer who is not
available for clarification on a line of code ...


l1:import model.Manager;
l2:public class DataSourceConnectionObject {
l3: public static void main(String[] args) {
l4: try {
l5: Manager manager =null;

...

is all that line 5 is declaring is a new instance of "Manager" and
naming it "manager" and assigning the expression a "null" value
without the "new" clause used to issue it?

any help would be nice
thx

manager is a reference variable of type Manager assigned the value of null.

That is often done before a try/catch block so that cleanup or other
code can be run in the catch or finally using that reference.

Manager manager = null
try {
manager = SomeManagerCreater();
// something that can cause an exception
} catch (SomeException se) {
if (manager != null)
manager.cleanup();
}

You'll see that a lot when opening streams for I/O.
 
L

Larry K. Wollensham

Eric said:
I am working from preexisting code by a programmer who is not
available for clarification on a line of code ...


l1:import model.Manager;
l2:public class DataSourceConnectionObject {
l3: public static void main(String[] args) {
l4: try {
l5: Manager manager =null;

...

is all that line 5 is declaring is a new instance of "Manager" and
naming it "manager" and assigning the expression a "null" value
without the "new" clause used to issue it?

Line 5 (which looks like "15" the way you've written it)

Not with the font I'm using here, which clearly distinguishes l and 1.
(Anyone else can quickly tell whether theirs does by looking at the
first two characters on the first line of the OP's code snippet.)
 
A

Arved Sandstrom

I am working from preexisting code by a programmer who is not
available for clarification on a line of code ...


l1:import model.Manager;
l2:public class DataSourceConnectionObject {
l3: public static void main(String[] args) {
l4: try {
l5: Manager manager =null;

...

is all that line 5 is declaring is a new instance of "Manager" and
naming it "manager" and assigning the expression a "null" value
without the "new" clause used to issue it?

any help would be nice
thx

Local variables must be initialized or assigned to before being used.
Variables may also be declared before a meaningful assignment is possible,
because of scoping; that is, the actual assignment may be within a block,
but you declare the variable outside the block so that other code can see
it...hence the initial declaration, and therefore because of the compiler
requirement that a local variable have a meaningful assignment before first
use, you'll see explicit assignments of default values. It's quite a common
thing to do.

AHS
 
D

Daniel Pitts

Larry said:
Eric said:
I am working from preexisting code by a programmer who is not
available for clarification on a line of code ...


l1:import model.Manager;
l2:public class DataSourceConnectionObject {
l3: public static void main(String[] args) {
l4: try {
l5: Manager manager =null;

...

is all that line 5 is declaring is a new instance of "Manager" and
naming it "manager" and assigning the expression a "null" value
without the "new" clause used to issue it?

Line 5 (which looks like "15" the way you've written it)

Not with the font I'm using here, which clearly distinguishes l and 1.
(Anyone else can quickly tell whether theirs does by looking at the
first two characters on the first line of the OP's code snippet.)
It looks close. At first I thought it was a snippet starting at line 11,
but then I noticed that it was an L, not a 1.
 
R

Roedy Green

l1:import model.Manager;
l2:public class DataSourceConnectionObject {
l3: public static void main(String[] args) {
l4: try {
l5: Manager manager =null;

He is setting the reference to point to no object at all. You
sometimes do this PRIOR to setting it to some code that might fail.
The value is then still defined, but null.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"In the central North Pacific, plastic outweighs surface zooplankton 6 to 1."
~ Thomas M. Kostigen
 
L

Lew

Roedy said:
l1:import model.Manager;
l2:public class DataSourceConnectionObject {
l3: public static void main(String[] args) {
l4: try {
l5: Manager manager =null;

He is setting the reference to point to no object at all. You
sometimes do this PRIOR to setting it to some code that might fail.
The value is then still defined, but null.

<http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.7>
<http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3.1>
<http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3.2>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top