Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

How to declare class  RSS feed

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.
I think the way in which i write programs is wrong. So i just need to clarify it. I use small alphabets for naming the class and sometimes i even write many classes in the same program(i.e in a single file). This has become a habit for me. I think there may be many more mistakes which i do but i dont even know. So please some one look at the code below and point towards the mistakes. I would be really a great help for me.




Thanks and Regards
alexander>
 
Java Cowboy
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

adeeb alexander wrote:Hi all.
I think the way in which i write programs is wrong. So i just need to clarify it. I use small alphabets for naming the class...


That is indeed not what the de-facto coding conventions recommend, and how classes and interfaces are named in the standard Java API. See Code Conventions for the Java Programming Language - those standards are used by most Java programmers.

... and sometimes i even write many classes in the same program(i.e in a single file). This has become a habit for me.


A better way is to put each Java class in a separate file, with a name that's the same as the class name (and the extention .java, ofcourse). Sun's Java compiler even requires this for public classes.

Looking at your code, here are some more tips: Make your member variables private, unless a more permissive access level is required. Currently, your member variables all have the default access level. Indent your code consistently, that makes it easier to read.

There is a great book about how to write good Java code: Effective Java. Buy it - you won't regret it, and it will make you a better Java developer.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class names always start with a capital letter. There is no compiler rule enforcing this, you are free to use whatever can be used for variables too, but it's called Java Naming Convention. Check here

http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html

Also, the Java Code Convetions will help you

http://java.sun.com/docs/codeconv/
 
Marshal
Posts: 61753
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean stylistic things? Do you really want us to look for things?

  • On-demand imports. Imports of single classes would be better.
  • Name of the class: Should start with a capital letter, should use whole words, should be a "noun phrase".
  • All fields except constants should be declared private.
  • Some identifier eg rcol colno, don't have capitalLetters for their 2nd words.
  • Identifiers for fields too short.
  • You should check whether any of those fields could to be local variables.
  • Mixing functionality. Don't have a display and connection to a database in the same class. Put separate things in separate classes.
  • Long statements correctly divided into lines, but the 2nd and 3rd lines should have more indentation.
  • Why are you using Vector rather than ArrayList?
  • Your List should be declared by its interface (List v = new Vector();)
  • Your List ought to have a type parameter <Foo>
  •  
    adeeb alexander
    Ranch Hand
    Posts: 268
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Ritchie.
    Can you please give an example for diff connection and display class. I mean how to declare connection in one class and display in other class and use them both to gether



    Thanks.
     
    Campbell Ritchie
    Marshal
    Posts: 61753
    193
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Not sure what you want, I am afraid. Please explain more.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!