How to access thread variables? [closed] - java

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How to access the thread variable from outside of the thread , I have the hashmap inside the thread which I want to access from my main program or service.
public class Sample {
class Thread {
//private synchronized hashmap declared here
}
}
I want to access the hashmap declared in Thread in other class lets say Class Abc

The real problem with multiple threads accessing data is synchronization. If you have a map with data, make it a ConcurrentHashMap, and place it so that you can access it. Now you have access to the data in your map. Note that there could be other dependencies in your code that require more synchronization, but at least accessing the data in the map is safe.
Update: In your case I would do something like:
public class Sample {
Map mMyMap = new ConcurrentHashMap();
void foo() {
// Access from here
}
class Thread {
// And from here
}
}
You can make it private, but there is much to say about inner classes and private that is out of scope of this question.

Related

Java Enum: Encapsulation and runtime initialization [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a funny question on the way home
I have an Enum
public enum Gender{
Yes(Constants.male()), female(Constants.female());
private final String value;
private Gender(String option){
value = option;
}
}
.. should I encapsulate value or just declare it as public?
Is there a disadvantage of run-time initializing the value?
In your case there shouldn't be much difference, the String class is immutable. It is recommended however as encapsulation is good practice.
As far as "run-time" init, I am not sure what you mean. Enum's as essentially singleton's so this would be inited when it is class-loaded. This is at runtime, yes, but there are no disadvantages; especially if you do not want to hardcode the values.
EDIT
As #GyroGearless points out the field should be declared as final, this is best practice even if it isn't public as it's a constant set in the constructor.

What is the explicit constructor access modifier? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Suppose there is a class A. Which of the following two access modifiers is a default one for a constructor?
public A()
{
private A()
{
//some code....
}
protected A()
{
//some code....
}
}
It means the exact same thing as modifiers to functions and variables, only now it refers to who can CONSTRUCT an instance of the class.
public - any one can call the constructor from anywhere in the code.
private - Unable to construct from outside the class - typically used to enable control over who gets to instanciate the class with the use of a static member factory method. A good example of an appication found here
protected - Like private but now inheritance is involved - any subclass factory method can be used because now they can call this constructor.
As #dasblinkenlight mentions, if you do not specify any modifier, then they default to being package-private, meaning they are only visible to classes within the package.

Accesing Private Methods in java? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
how to implement such a functionality to access private members ?
Java checks access permissions during compilation only. Are you surprised? I was surprised very much to find out this fact.
So you can create skeleton of the third party class (even with empty implementations.). The interesting method should be protected instead of private. Now write your subclass and compile it against your stub. Then package only your subclass and try to run it with the "real" class. It should work.
I have tried it when I had to access private method or field and it worked fine for me.
ref. https://stackoverflow.com/a/4440051/1312423
Java checks access permissions during compilation only. Are you surprised?
Yes, because it checks the access modifier at runtime as well.
I start with
public class AnotherClass {
protected static void printMe() {
System.out.println("Hello World");
}
}
public class Main {
public static void main(String... args) {
AnotherClass.printMe();
}
}
and it compiles and runs.
If I change the printMe() to be private without re-compiling Main it does compile but when I run Main I get.
Exception in thread "main" java.lang.IllegalAccessError: tried to access method AnotherClass.printMe()V from class Main
at Main.main(Main.java:22)

Static Util methods vs Instance Util methods - Which one is good? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Keeping Util class methods static is a good practice or instance methods are good?
If a method is an utility method then it has no meaning in being associated to an instance of an object so there is no reason to make it an instance method.
An instance method should be something that is meaningful to a specific instance of a specific class.
Usually, you have an Util class with static methods so that you don't need to create an instance of your Util class.
I don't see the point in having an instance of an Util class, so I'd say keep the methods static.
if your method is not depend on other non static member. your method should be static.
i think here you are making utility pack so. method should static if it is not depend on non static member :)
generally util has no instance of util class

Java 7 Objects class [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
There is a public final class Objects in Java 7 that extendes the java.lang.Object base class.
Will this by default be the base class like or do we need to call the method on "Objects" class and call the corresponding methods?
No. Nothing changes. The Objects class is simply an utility class. It has only static methods, it's in the util package, and does what ObjectUtils from commons-lang is doing.
Objects is final and it extends Object giving you null safe utilities.

Categories