Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

Hayxe laurus
Hayxe laurus
7,285 Points

What does the final keyword actually do in java and for android development?

I haven't encountered a problem however I am highly on what it does so I may know if I need to use it or not in the future.

2 Answers

I believe Dai has already given you an apt answer to the question however I would like to add to it based on three different uses that final offers in Java thus android.

1) Defining a variable - you can declare a variable as final.

final int USER_ONE = 1;

When we declare a variable as final its value will never change through out the entire program. Also by convention in Java final variables names are written all in capital letters

2) Declaring a method as final - you can also declare a method as final. When a method is declared final it cannot be overridden

class A {
  final void hello(){
      //some code
   }

class B extends class A {
  void hello() {  // this line will generate an error saying hello cannot be overridden
   //some code
   }

3) Declaring a class as final - when you declare an entire class as final that class cannot be inherited by another class

final class A{}

class B extends class A {  // this will give a compile time error since A cannot be inherited since its final
//class memebers
}

Hope that explains. Ask again if you need more info.

Dai Phong
Dai Phong
20,395 Points

final is a keyword of the Java language, it define final variable (does not allow to be override later), you can use it if you want to define a variable which does not allow other to override it