Nathan
Nathan

Reputation: 858

Is it against Java conventions to name an object the same as the class name, sans the capital?

If I have an object Mode, would it be acceptable, according to common/standard conventions, to call the variable mode? While I was bringing up a hypothetical situation during my CS class (high school), my teacher interrupted me to ask why I would call an object the same name as the class, without the capital? (This is very much taken out of context, but my hypothetical situation was irrelevant to this.)

Also, I already understand that if my teacher says something like this, I should generally just do it to conform to her desired conventions. I am just wondering if in general, it is acceptable.

Upvotes: 4

Views: 1393

Answers (3)

Kninnug
Kninnug

Reputation: 8053

As far as I can see it acceptable, note however that it can be confusing, especially if you're using more than one object of that class.

Upvotes: 3

Yogendra Singh
Yogendra Singh

Reputation: 34367

I think this is completely fine. This is normal practice. Class instances are most of the time named same but start in lowercase.

General naming pattern is to name the classes and variables with little descriptive names to increase the readability and maintainability. As long as naming is self explanatory, its fine.

Upvotes: 15

CaTalyst.X
CaTalyst.X

Reputation: 1665

I would say that follows popular convention. The idea is that if you have to name the variable something different than the class name, then you should probably change the class names.

Using good, descriptive names makes for readable and maintainable code.

Upvotes: 3

Related Questions