• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Intent Constants

Hi,

So I'm developing my first "todo-list" application and I came across this video. I'm really confused to what these "intent constants" actually do and why he's made them in a separate class file? Any help would be appreciated as I'm kinda struggling.

link to Youtube video:
Thanks!
 
Please indicate at what point in the video he talks about this, because I'd rather not sit through a 34 minute video. Thanks.
 
Ok, it's at about 11:25 where he introduces this. He creates a class called Intents_Constants.
The reason for using such a class is to contain a bunch of static variables. What's a static variable? Well it relates to the scope of variables in Java. To cut a long story short, when you declare a variable in your code, it has a visibility. The visibility is controlled by the keywords you use in the variable declaration, and also the place at which you declare the variable.
If you declare a variable in a method, it's only visible to code within that method

Code:
public void my_method() {
  String localVariable;
  ...
}

So 'localVariable' can be used within my_method(), but outside this, it's completely unknown.

You can also declare what's called a class variable. That is a variable which is visible to all methods within the class e.g.

Code:
public class MyClass {
  private String classVariable;
  ...

  public void my_method() {
    // classVariable can be used here
  }
}

But private class variables are only visible to code within that class, and only become created when you create an instance of that class. If you wish to use a variable throughout your code, without having to create an instance of a class, you can do that by declaring it public static. But it has to go inside a class somewhere. So you can define a container class for this purpose e.g.

Code:
public class MyConstants {
  public static final String globalVariable = "Hello";
  ..
}

This means that I can use 'globalVariable' at any point, anywhere in my application code, like this

Code:
public void my_method() {
  Log.d(MyConstants.globalVariable);
}

The point is, I haven't created an instance of the class MyConstants in order to access and use the globalVariable.

Some purists would say that doing this breaks encapsulation, which is a key principle of O-O software design. But sometimes it makes sense to do it. I don't have a problem with using public statics like this.
 
Yeah sometimes it's the stupid shit that annoys me. Feel free to come back and ask questions. I'm always here. :)
 
Back
Top Bottom