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)
Related
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.
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.
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
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.
My classpath
E:\classes\sample
I am running from c:\Program Files\Java\jdk1.4\bin
Source Code for E:\classes\sample\Test1\sample1.java
package sample.Test1;
class Sample1
{
public void printThis()
{
System.out.println("WE are in sample1");
}
}
Source Code for E:\classes\sample\db\sample2.java
package sample.Test2;
import sample.Test1.Sample1;
class Sample2
{
public static void main(String args[])
{
System.out.println("Main Class \n");
Sample1 s = new Sample1();
s.printThis();
}
}
When I am compiling Sample2.java it is showing error. Sample1.java already compiled. Please help me..
The reason it doesn't compile is your have default visibility on your first class (not public), so it can only be "seen" by classes in the same package. Your second class is in a different package, so it can't use it.
To fix, add the keyword public to your classes:
public class Sample1 {
Edit:
Still got a problem? I would just use an IDE like Eclipse. It will tell you what the problem is and help you fix it.
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.
Right now I am confused with inheritance and object of class. We can do the same using object as inheritance does.
ok here is first code
public class MainForm {
GUI g = new GUI();
g.show();
g.destroy();
}
class GUI {
void show(){
......
}
void destroy (){
......
}
}
now second one
public class MainForm extends GUI {
//GUI g = new GUI();
void show();
void destroy();
}
class GUI {
void show(){
......
}
void destroy (){
......
}
}
both code do the same.Right? then
Why should I extend a class when i can do the same using an object of class?
Inheritance is often used when you want to create multiple classes of a parent class, without duplicating a lot of logic. I may have a class named Animal which has methods speak(), walk(), and sleep(). I also may want to have specific instances of Animal like Cat and Dog.
Rather than implementing all three of those methods individually, I can write them in Animal and then have my other classes extend Animal to make use of those, as well as add any methods specific to those classes (like claw() or fetch()).
The benefits are reduced code duplication and better 'abstraction' of the objects. However, there are some drawbacks as well, as this article points out. It is important to know when inheritance is useful, and when it is not.