• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

How to find unused variables in Java program?

Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an assignment to write a parser which will find unused variables in our code. I am totally dumbstruck since I don't have any approach. Could you please provide me any pointers to proceed with my assignment
Posts: 1868
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may want to use either a class which implements Map or a class which implements Dictionary to find all of the variables in the source code.
Once you have a collection of all of the variables then you want to count the number of occurrences of each variable.
Now that you have the count you can see which variables are used and which are not.

Here is some extra reading which may help out
Collections Documentation
Collections Tutorial By Oracle
Saloon Keeper
Posts: 10834
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting problem. This could be either quick and dirty, or complex and complete. @Pete's approach is one simple way and may suffice for an assignment.

The ultimate and non-trivial way would be to have a Java parser implemented with something like Antlr. Then analyze the parse tree. A Java language definition suitable to use as a spec that can be fed into Antlr can be found online but I've found those to sometimes be incomplete so you may end  up searching for one that works. Then the analysis involves traversing the parse tree.

A middle of the road approach would involve first writing a filter that removed all comments and string literals. You don't want to be counting occurrences that appear in those. This is trickier than it sounds but once written can be used in creating a variety of analysis tools.

Then find all words that begin with a lower case letter. This is not fool proof as Java compilers will accept variable names that begin with upper case letters. It is only by convention that variables begin with lower case letters.

Eliminate keywords.

Next, eliminate words that are immediately followed by an open parenthesis, those would be methods, not variables.

Next, keep track of scope. Variables defined within blocks ({...}), variables defined as method parameters, and class level variables.

See if you can detect assignment to a variable vs a declaration vs being used.
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic