2

I'm extremely new to Java and Android development and recently did a quick skim over the documentation to begin learning Android development. These should be very basic clarity-questions, but I can't find the right place to search to get the answer.

I was hopping I could get some clarification on the syntax in this android tutorial:

All of the private variables are declared at the top of the class and prefixed with "m". Why is this? What does the m-prefix imply?

private DrawerLayout mDrawerLayout;

When these m-prefixed variables are declared inside of the method, why are their parent objects written with a parenthesis?

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

I'm a bit confused over this because I see objects being instantiated with the new keyword. Is this different from what's going on above?

And my last question, why are there arrows >< wrapped around String

new ArrayAdapter<String>

Here's the full example code

public class MainActivity extends Activity {
    private String[] mPlanetTitles;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    ...

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPlanetTitles = getResources().getStringArray(R.array.planets_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mPlanetTitles));
        // Set the list's click listener
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        ...
    }
}
Dakkaron
  • 5,930
  • 2
  • 36
  • 51
user2989731
  • 1,299
  • 3
  • 17
  • 33

2 Answers2

3

All of the private variables are declared at the top of the class and prefixed with "m". Why is this? What does the m-prefix imply?

It stands for "module level". It's a widely used convention. You are not forced to use it.

When these m-prefixed variables are declared inside of the method, why are their parent objects written with a parenthesis?

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

This is a cast to the type you specify in the parentheses.
In this example, the whole line means:
Find the view called drawer_layout in the currently assigned layout and convert it to type DrawerLAyout, then assign it to the local variable mDrawerLayout.

I see objects being instantiated with the new keyword.

new creates a new instance (copy) of the object

And my last question, why are there arrows >< wrapped around String new ArrayAdapter

It means "of type String".
So, this ArrayAdapter in the example will contain String elements.

Community
  • 1
  • 1
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 5
    Doean't the m stand for member variable? – Maze Jul 27 '15 at 19:21
  • Also. It depends on how you read it. Since it's a conventional definition only. It's a suggestion. Another convention uses "_" and another convention calls these "fields". – Phantômaxx Jul 27 '15 at 19:23
  • 2
    Android Studio (Gradle) has a different definition of a module (library module, application module, test module). I would say the m stands for member. It's also a fairly controversial convention, the argument against it being that any decent IDE will highlight your member variables for you so the prefix is redundant. – Charles Durham Jul 27 '15 at 19:53
  • It does not stand for member variable. The word member is rarely used in reference to java, because literally everything is a member of something in Java. – kingfrito_5005 Jul 27 '15 at 19:56
  • @kingfrito_5005 The fact that the OP is a newbie necessitates calling out your completely wrong comment. The "m" convention *does* stand for member, and the word "member" is used quite often in reference to Java, with a very specific meaning. – Kevin Krumwiede Jul 27 '15 at 22:37
  • @kevin Krumwiede, please explain what this specific meaning is, as I have never heard the word member used in reference to java except for the singular case of member classes. – kingfrito_5005 Jul 28 '15 at 13:08
  • @kingfrito_5005 Constructors, methods, and fields are collectively called members. [The documentation](https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) refers to them this way, and there's a [reflection interface](http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Member.html) with corresponding implementations. The term "member class" seems to have been coined by the user community. – Kevin Krumwiede Jul 28 '15 at 17:45
  • In a strict technical sense yes, but Ive never heard anybody except C++ programmers call any of those things members. They are usually just called Constructors methods and fields. The only reason member class seems to have gained steam is because classes are sometimes not members of classes or objects, whereas everything else is. – kingfrito_5005 Jul 28 '15 at 17:57
  • So a succinct definition of "member" might be *identifiers declared in the class scope.* – Kevin Krumwiede Jul 28 '15 at 18:01
0

Who says it comes from Java that naming convention? As AFAIK it originates feom C++, but at least this confirms I am right. That thread also diacusses the usefullness of the prefix

Updated: sorry that was intended as a comment :-( Don't know how to convert it

Community
  • 1
  • 1
Maze
  • 726
  • 6
  • 9
  • for future reference, the thing to do woul djust be to copy/paste your post into a new comment then delete the old one. Theres no way to just convert it (that Im aware of) – kingfrito_5005 Jul 28 '15 at 13:14