Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's is the best practice for marking variables that you need to delete later in Java? [closed]

Tags:

java

I scratched my head for two weeks trying to compute something based a variable.

It turns out I set the variable earlier to solve temporarily another problem and never went back to correct it.

Normally, I try to mark up code with //ToDo 's to remind me to remove the temporary variable.

In this case, I didn't mark it since I was skipping around trying to fix more than a few things. (I couldn't figure out what was going on so I was trying all sorts of stuff!)

How do you mark temporary variables that you wish to remove later?

  1. Do you instantiate them as private at the top of the class?
  2. Mark them inline with something like //Delete Me Later

What's is the best practice for marking variables that you need to delete later?

(short of having a really organized brain of course...)

like image 893
John Stack Avatar asked Aug 10 '12 12:08

John Stack


People also ask

How do you clear a variable in Java?

Set clear() method in Java with Examples Set. clear() method is used to remove all the elements from a Set. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing Set.

How do you delete in Java?

In Java, we can delete a file by using the File. delete() method of File class. The delete() method deletes the file or directory denoted by the abstract pathname. If the pathname is a directory, that directory must be empty to delete.

How do you nullify or remove the unused objects from memory?

In java, garbage means unreferenced objects. Garbage Collection is process of reclaiming the runtime unused memory automatically. In other words, it is a way to destroy the unused objects. To do so, we were using free() function in C language and delete() in C++.


2 Answers

Use the annotation @Deprecated

@Deprecated
public void Test() {
  //
}
like image 69
oopbase Avatar answered Oct 23 '22 15:10

oopbase


Found it after a bit of googling, as i was curious also.

@Deprecated
public void speak() {
   System.out.println("Meow."); 
}

From Wikipedia, on Java annotations:

Java defines a set of annotations which are built into the language. Annotations applied to java code: @Override - Checks that the function is an override. Causes a compile warning if the function is not found in one of the parent classes. @Deprecated - Marks the function as obsolete. Causes a compile warning if the function is used. @SuppressWarnings - Instructs the compiler to suppress the compile time warnings specified in the annotation parameters Annotations applied to other annotations: @Retention - Specifies how the marked annotation is stored—Whether in code only, compiled into the class, or available at runtime through reflection. @Documented - Marks another annotation for inclusion in the documentation. @Target - Marks another annotation to restrict what kind of java elements the annotation may be applied to @Inherited - Marks another annotation to be inherited to subclasses of annotated class (by default annotations are not inherited to subclasses).

like image 21
Viezevingertjes Avatar answered Oct 23 '22 15:10

Viezevingertjes