• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

final instance variable  RSS feed

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Logger
{

private static int loggerCount = 0;

private final int logLevel;

private Logger(int logLevel) throws IllegalArgumentException
{
if(logLevel < 0 || logLevel > 10)
{
throw new IllegalArgumentException("logLevel can only take values 0 to 10");
}
this.logLevel = logLevel;
}

public static Logger createInstance(int logLevel)
{
Logger retObj = null;
try
{
retObj = new Logger(logLevel);
}
finally
{
loggerCount++;
}
return retObj;
}

}

how it is working ???
as we know that final member variable of type int has no default value , it need to be explicitly assigned the value at the time of declaration or in the constructor of the class.

correct me if i am wrong
 
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

or in the constructor of the class


Isn't that exactly what's happening?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inside Logger constructor:

this.logLevel = logLevel;
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Golden rule regarding default values of fields is::

If no intialization is provided for an instance variable either in the declaration or in constructor or in instance initiatlizer block then it is automatically initialized with the default value of its type when the class is instantiated.

So in above program, the instance variable is initialized with default value = 0.(int default value)

I hope that explains.

Ashu
- SCJP 1.4
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private Logger(int logLevel) throws IllegalArgumentException
{
if(logLevel < 0 || logLevel > 10)
{
throw new IllegalArgumentException("logLevel can only take values 0 to 10");
}
this.logLevel = logLevel;
}


In this constructor you are assiging the value to the logLevel . before this you are not using it thats why is not giving you the error otherwise it will give you the error The blank final field logLevel may not have been initialized
 
sweety sinha
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys, i didn't read it correctly
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the variable logLevel is declared as final?
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi....
i just faced the same problem of declaring final variables...
we have to initiaze at the same time as well as the same line where it is declared..otherwise it eill give the compiler error...the variable might not have been initialised....
 
Sheriff
Posts: 9613
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even when you initialize a final constant at the time of declaration, you have to initialize it with another compile time constant or literal to make it a compile time constant.

int a = 10;
final int b = a;//not a compile time constant....
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!