Calling another method into my class - java

I would like to implement a method that I use in another class in my project package.
The class that I would like to add the method to does not extend the class where the method comes from.
I've tried:
MyMethod p = new MyMethod;
When I do this I get 'cannot resolve symbol 'MyMethod'

The statement
MyMethod p = new MyMethod
is syntactically incorrect. If MyMethod is a class , and if you want to create an instance of it to call any method of it :
Then the correct syntax to instantiate would be :
MyMethod p = new MyMethod();
Then you need to implement methods and call it with the newly created instance p.
If you are asking about calling a method from different class existing in a different package, you 1st have to import that class in your MyMethod class, then have to create an instance of that class, or cast with that class to be able to call the method.

May be the method is private . U cannot call private method in other class.
This is simple:
Class A{
public void methosAA(){
}
}
Class B{
A a=new A();
public static void main(){
a.methosAA();
}
}

Related

Why does this.getClass give it's own class name rather than Anonymous class name?

I have created anonymous class by implementing interface I inside public static void main() method. So, by java 8 for the abstract method test(), the implementation is provided from imple() method of class C.
So, inside public static void main() method, printing _interface.getClass(), I got
package_path.Main$$Lambda$1/310656974 which is absolutely fine. Bacause it print's the anonymous class name.
Also, _interface is pointing to an anonymous object in heap and hence I'm doing _interface.test();
So, the first statement that test() method has now is to print the class name,
But eventually what it print was,
package_path.C (telling me C is the class name). How is that possible? Shouldn't package_path.Main$$Lambda$1/310656974 be printed again? Because 'this' means anonymous inside the test method right?
#java.lang.FunctionalInterface
interface I {
void test();
}
class C {
void imple() {
System.out.println(this.getClass());
System.out.println("Inside Implementation");
}
}
class Main {
public static void main(String[] args) {
I _interface = new C()::imple;
System.out.println(_interface.getClass());
_interface.test();
}
}
Hopefully, this might help you understand, that when you declare
I _interface = new C()::imple;
you've actually implemented the interface somewhat similar to (though not same as):
I _interface = new I() {
#Override
public void test() {
new C().imple(); // creating an instance of class `C` and calling its 'imple' method
}
};
Hence when the test method is called, it first creates an instance of C which prints
class x.y.z.C
as the class.
Because 'this' means anonymous inside the test method right?
Now as you can see above, there is no more anonymous class from which imple
is being called from, hence this is not representing the anonymous class anymore.
As Holger clarified in comments further, despite the representation as lambda or anonymous class at the calling site, the this.getClass() inside a method of class C will evaluate to C.class, regardless of how the caller looks like.
Recommend: Continue to read and follow on Is there any runtime benefit of using lambda expression in Java?

Why I can't call a static method in another class?

I have a class A with static method like this:
public static class A {
public static void methodA(){...}
when I want to call methodA from class A in class B ulike this:
class B {
A.methodA();
}
the IDE says it cannot reslove reference with methodA, I know it's java syntax problem and what can I do to call methodA in class B except call it inside class B's method?
You can't call method in the body of class, as you did it in class B. In the body of class you define fields and methods of this class. If you want some actions to be performed while creating instance of some class, you need to contain these actions in the constructor, in an intialization block or in the body of additional method. Calling methods in the constructor seems to be risky if object is not being created successfully in any circumstances, it might cause problems with calling method contained in the constructor.
To call methodA() I suggest one of the following ways to achieve that:
Create appropriate method in class B and call static method of class A in the body of class B.
Create a proper initialization block to call this method.
Examples of how to call methodA() from class B, you can see below:
// 1.:
class B {
public void callA() {
A.methodA();
}
}
or
// 2.:
class B {
{
A.methodA();
}
}
Besides what the accepted answer by Przemysław Moskal states, you can also call static methods from a static block before creating any instances of class B:
class B {
static {
A.methodA();
}
}

Overriding private method

We can not override a private method then why does the followng code does not give an error. Instead it produces the output.
class A {
private void fun() {
System.out.println("ths is a private method");
}
}
class B extends A {
void fun() {
System.out.println("ths is wrng");
}
}
class C {
public static void main(String args[]) {
B ob = new B();
ob.fun();
}
}
private methods are not inherited. In your main method you're invoking the fun() method on a variable of type B. The fun() method of type B seems to be accessible, assuming your class B and class C are in the same package.
Had you done this
A ob = new B();
ob.fun();
Then you would have gotten your compilation error.
B#fun() is completely unrelated to A#fun().
Both are completely different methods and not related to each other. As private methods are not inherited there is no concept of overriding here.
Method fun() is not overridden in B class because it was never inherited from class A.
What you are invoking in your code is method of B class and that has no relation with class A and method A#fun().
To check this, Add a #Override annotation and you will get compile time error.
// Compilation error.
Class B extends A{
#Override
void fun(){
System.out.println("ths is wrng");
}
}
Now this code will not compile.
First of all, private methods are not inherited in child classes. If you still try to write a method with same name as that of parent class, java compiler will consider the method in child class as a completely new method and will not throw any exception. Its like the new method in child class shadows the private method in parent class.

Why can't I create a new method in an anonymous inner class?

If I have the following class:
public class TestObject {
public String Hooray() {
return "Hooray!";
}
}
I can obviously instantiate the object, and I know some sort of subclassing must be going on since I can override the Hooray method, but if there's subclassing, why can't I create a new method inside the anonymous class?
TestObject a = new TestObject() {
public String Boo() {
return "Booooo";
}
};
System.out.println(a.Boo());
returns a syntax error
You can create the method, there's nothing wrong with your Boo method (apart from the fact that it has a capital letter at the front). The problem is that outside of the anonymous class, the Boo method is not available (it is not exposed as part of the API of the class).
This is the same with any class that implements an interface... if the class has methods (even public methods) that are not part of the interface then you need to cast the instance to the specific class in order to access these methods.
Unfortunately, because this is an anonymous class, you can't cast it (you don't know what to cast it to).
These internal methods can still be useful, but you have to call them from inside the anonymous class, not from outside.
Because the class has no name, you cannot refer to its type definition at compile time. The compiler can only know it as a TestObject, which has no boo() method
You have this:
public class TestObject {
public String Hooray() {
return "Hooray!";
}
}
TestObject a = new TestObject() {
public String Boo() {
return "Booooo";
}
}
System.out.println(a.Boo());
You can't do this. You can create new methods in anonymous inner classes, and, in fact, you are. But you wouldn't be able to call a.Boo() from outside, since a is a TestObject and TestObject has no method named Boo. It's the same reason you can't do this:
public class Base {
public void something ();
}
public class Derived extends Base {
public void another ();
}
Base b = new Derived();
b.another(); // b is a Base, it must be cast to a Derived to call another().
In the above you have to cast b to a Derived to call the new method added to the derived class:
((Derived)b).another();
The reason that you couldn't do this with anonymous inner classes (which are just syntactic shortcuts for deriving new subclasses) is precisely because they are anonymous - there is no type available for you to cast them to.
The reason you can't access another() through type Base, by the way, is pretty simple when you think about it. While Derived is a Base, the compiler has no way of knowing that Base b is holding a Derived as opposed to some other subclass of Base that doesn't have an another() method.
Hope that helps.

java method local inner class

i am trying to pass method local inner class object as an argument to some other function either in the scope of outside class or out of that class
public class MethodClass {
public void p(){
class h{
public void h1(){
System.out.print("Java Inner class");
}
}
h h2=new h();
}
}
here h2 i want to pass to any other function in the same class MethodClass or out of that class. can any one give me the procedure to pass the argument in that way?
If another method needs to know about the class, then you shouldn't declare it within a method, basically. Make it a nested class within the class itself, or even a top-level class.
I can't say I've ever used method-local class declarations.

Categories