Java variables 'm' and 'p' prefixes
Asked Answered
M

4

5

What are the purposes and meanings of this syntax? I'm seeing lines like private Object mSelectedOperationRow; and mMessageService = pMessageService;. More complete code below.

public class SearchResponseBean extends BaseBean implements Serializable,
    SearchResponse {

@Autowired
private SearchLifecycleService mSearchLifecycleService;

@Autowired
private ConfigurationServiceImpl mConfig;

@Value("#{sessionBean}")
private Session session;

@Value("#{searchRequestBean}")
private SearchRequest searchRequest;

@Value("#{searchResponseFilterByBean}")
private SearchResponseFilterBy searchResponseFilterBy;

@Value("#{searchHistoryBean}")
private HistoryBean<SearchHistoryItem> historyBean;

@Autowired
private SearchResponseDataModelFactory mSearchResponseDataModelFactory;

private int mCount;
private DataModel mDataModel;
private SearchPerspective mSearchPerspective;
private Operation mSelectedOperation;
private Object mSelectedOperationRow;
private List mSelectedList;
private List<String> mAvailableOperations;
private List mFilteredList;
private boolean mRelatedDocSearch;

private transient MessageService mMessageService;

public SearchResponseBean() {
    mMessageService = new MessageServiceImpl();
}

public SearchResponseBean(MessageService pMessageService) {
    mMessageService = pMessageService;
}

protected void init() {
    String THIS_METHOD_NAME = "init";
    mFilteredList = null;
    mSelectedList = null;
    mSelectedOperationRow = null;
    mSelectedOperation = null;
    mCount = -1;
    mDataModel = null;
    mSearchPerspective = null;
    mAvailableOperations = null;
    mRelatedDocSearch = false;
    logger.debug(getClass(), THIS_METHOD_NAME,
            "Initialized with null/defaults. ");
}
Micromho answered 10/11, 2014 at 15:51 Comment(3)
My eyes... It is using hungarian notation with m = member and p = parameter (I suppose).Chestnut
variables names are just, well, variable names. nothing special about themNotability
This is a coding convention (one of many) to distinguish parameters (pXXXX) from member variables (mXXXX). Less popular today.Nicholas
I
12

This is not a syntax thing, its a project guideline thing. Some projects have naming conventions for variables, members, arguments ... and more.

  • m simply stands from member,
  • p stands for parameter.

Its meant to make it easier to distinguish between local variable, members and method arguments.

Inspector answered 10/11, 2014 at 15:54 Comment(0)
R
9

The m and p prefixes aren't part of Java's syntax per-se, they're just a convention used in some projects. m is short for "member" and p is short for "parameter". This way, when reading a long block of code, it's clear where each variable comes from, and what it means. Modern IDEs color members differently so this convention may not be as useful as it used to be, but it's still employed in some projects.

Raddled answered 10/11, 2014 at 15:54 Comment(1)
+1 The last sentence should really be highlighted. Any decent Java IDE allows to use different formatting for fields, local variables and parameters, so the argument of "recognizing where the variable comes from" is no longer valid. Since variable prefixes are already a quite uncommon convention in Java code, you might even confuse people who read your code (as this question shows).Sejm
X
3

Its an example of Hungarian Notation, a naming convention which is rarely used these days in Java code.

Xerography answered 10/11, 2014 at 15:54 Comment(0)
H
2

Well these are just naming conventions.

  1. Usually 'mXYZ' is used for member variables of a class.
  2. And 'pXYZ' is used for argument of a method.

I think these are just leftover of C++ world or may be very early IDE versions of java, where it was probably hard to look at 'formatting' of a variable and identify if this is a local variable or a class level one.

Using 'm' and 'p' prefix these days, in my opinion, is just redundant with modern IDEs.

Habitation answered 10/11, 2014 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.