Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes compile error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "compile error" Watch "compile error" New topic
Author

compile error

sahan thinusha
Greenhorn

Joined: Nov 10, 2012
Posts: 10


"Example.java:10: error: variable d might not have been initialized"
why?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42948
    
  70
Imagine what happens if a > 2.

Is this a typo: "if(a<2)d='j'; if(a<2)d='k';" ? As it is, the first statement will have no effect.
Tomek Ziolkowski
Greenhorn

Joined: Mar 07, 2012
Posts: 9

The 'd' variable must be initialised outside of the if condition or there must be added 'else' similar as with initialisation of 'c' to any of the conditions at lines 7,8,9.It seems that a compiler doesn't know whether an if condition is passed at line 10 and thus takes a possibility that this condition may not be true.
sahan thinusha
Greenhorn

Joined: Nov 10, 2012
Posts: 10

sorry my mistake.this is the code.
sahan thinusha wrote:

"Example.java:10: error: variable d might not have been initialized"
why?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

Because the compiler is too stupid to see that all cases are covered. We see it, but because there are three different if-statements the compiler doesn't.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40058
    
  28
Don’t call the compiler stupid. It is very difficult to program a compiler to verify that those cases cover all possibilities.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42948
    
  70
"Not smart enough for this task" is the same as "too stupid for this task", isn't it?
Paul Witten
Ranch Hand

Joined: Oct 10, 2012
Posts: 86
Ulf Dittmer wrote:"Not smart enough for this task" is the same as "too stupid for this task", isn't it?

I call it too smart to get involved.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40058
    
  28
Agree with Paul Witten. It would be possible to write a compiler which can cope with all sorts of daft code, possibly running through all paths of execution to test whether the value has or has not changed from 2. Of course, it could then take several minutes to compile a 50‑line class, so Paul Witten’s comment is spot on.
Paul Witten
Ranch Hand

Joined: Oct 10, 2012
Posts: 86
Campbell Ritchie wrote: Of course, it could then take several minutes to compile a 50‑line class

All joking aside and all trivial code aside, the Big World does have to build systems such as Weblogic Server which already takes a couple of hours on a good box. It's unimaginable how that logic overhead would affect large projects. It might make them unbuildable in our lifetimes. I suppose that's good job security. Or maybe the opposite. "OK, we kicked off the build. You're now laid off because by the time the build is done you'll be 80 years old."

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40058
    
  28
Paul Witten wrote: . . . It's unimaginable how that logic overhead would affect large projects. It might make them unbuildable in our lifetimes. . . .
You could probably apply that to Trojans, etc, and we could build anti‑malware programs quicker than the malware.

Joking aside, it would probably run in exponential complexity, so 80 years is quite feasible.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compile error