Win a copy of Rust Web Development this week in the Other Languages forum!

Richa Sinha

+ Follow
since Sep 14, 2012
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Richa Sinha

Your code where you are assigning memory to a variable at the class level will give stack overflow error.
It is because when you assign memory to an object, it allocates memory space to all the instance variables and the functions.
In you case you are creating an object "a" of class X. When you do this, the JVM will start allocating memory to the instance variables.
In your case, "b" is an instance variable to which you are assigning memory at the class level. Since "b" is also an object, the JVM will
again go to assign memory space to the instance variables of class X (which is b in our case). Hence this process will keep on going
till you run out of memory.
So in order to avoid this problem, if you are creating an object of a class as its instance member, avoid assigning memory to it at the class level
or in the constructor.
9 years ago
Yes, you can declare variables outside a method in java. Also, you can create an object and acess variables and methods outside a class. The following example illustrates the same. Suppose you have a class Test and class Sample as follows:

9 years ago
The web applications are multi-threaded, so we don't need to explicitly use threads there but all the applications that we develop are not web applications that use JSP or Servlets.

Threads are used when you want to execute two or more tasks in parallel. One example where threads are required could be an application where the application loads something from the internet on the startup and we want the loading icon to come during the loading time. Here there are two tasks running in parallel. Suppose we have a function public void loadFromInternet() that loads the data from internet and we have one more function public void showLoadingScreen() that shows the loading icon. Now if we call the loadFromInternet() and showLoadingScreen() function one after the other, then the first of all, the data will be loaded and after that the loading icon will be displayed, which is not what we want. We want that both the functions should work at the same time. Here threads come to rescue. We can create two threads and then call the two functions, one on each thread and acheive our desired goal.

But while using threads, we need to take care of the synchronization issues as well. We need to ensure that two threads should not modify the same resource at the same time.
Abstract classes are those classes which contain abstract, i.e. non-implemented methods. Abstract classes are mainly used in inheritance using which we can acheive type compatiblity and extensibility.
It can be better expressed as follows:

Suppose you have a student class which is abstract having a method public abstract void study(). Also, there is one CollegeStudent class and one SchoolStudent class, both of which extend the Student class. Now both the CollegeStudent and SchoolStudent both study but in a different manner. So they both override the method study from the Student class and write the implementation specific to them.
Hence, we can conclude that using abstraction we can acheive extensibility and type compatiblity, which are the main use of inheritance.
9 years ago
You can null out an array just by making it point to NULL.
E.g. Suppose you have an array like

String []arr=new String[10];
// A few lines of code.
// Now you want to garbage collect the array.
arr=null; //This line will make your array available for garbage collection.

You don't need to iterate through the array and make every instance point to null. A single line of code making the whole array object pointing to null will also work.
9 years ago