What is the equivalent of :: operator in java?

Firdaws

When accessing a function from another class in c++, we can write: classA::fct();

Is there an equivalent operator in java? If not, how can we access a function from another class in java?

Christian Eriksson

Well the ::-operator (Scope Resolution Operator) in C++ allows you to resolve ambiguous calls/references to identifiers. However, in java there are no independent functions as all functions are actually methods (members) of a class. As such there are no need for this operator, have a look here for differences between Java and C++ classes.

I am guessing you are attempting to access a member (possibly static) of a class, in which case you'd use the .-operator as exemplified in Mwesigye's answer or as follows:

public class AB {
    public static void main(String[] args) {
        B myB = new B();
        myB.printA();
    }
}

public class A {
    public static int getInt() {
        return 4;
    }
}

public class B {
    public void printA() {
        System.out.println(A.getInt()); // output: 4
    }
}

Here the .-operator is used to access printA() from the instantiated object myB (instantiated from class B). It is also used to access the static method getInt() whose implementation is tied to class A rather than any object of A. More info can be found here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

what is the equivalent of :: operator in D?

From Dev

what is the equivalent of += operator in clojure

From Dev

What is the BINARY operator equivalent in JPQL?

From Dev

What is the JavaScript equivalent for Swift ?? operator

From Dev

What is the BINARY operator equivalent in JPQL?

From Java

What is the Java equivalent for LINQ?

From Dev

What is the 'defer' equivalent for Java

From Dev

What is the ruby equivalent of this java loop since there's no pre-increment operator?

From Java

What is the equivalent of the join operator over a vector of Strings?

From Dev

What is the Python numpy equivalent of the IDL # operator?

From Dev

What's the equivalent of a spread operator (...) for objects properties?

From Dev

What is the equivalent of the "is" operator for reflected generic types?

From Java

what is '^=' operator in java

From Dev

what is '^=' operator in java

From Dev

What is the purpose of "^= "operator in Java?

From Dev

What is "~" operator in Java

From Dev

Java equivalent for C++ operator overloading

From Dev

Java equivalent of C++ copy assignment operator

From Dev

Scala equivalent of Java 8 :: (double colon) operator

From Dev

Java equivalent for C++ operator overloading

From Dev

What is the Java equivalent of this Scala code?

From Dev

What is the equivalent of javascript setTimeout in Java?

From Dev

What is the equivalent of stringByFoldingWithOptions:locale: in Java?

From Dev

Java 8: What is the equivalent of "UseSplitVerifier"?

From Dev

What is the equivalent of Java Object in Swift?

From Dev

What is the Python equivalent of RequestConfig in Java?

From Dev

What is the Java equivalent of this Haskell function?

From Dev

What is the equivalent of Java Scanner in Kotlin?

From Dev

What is the equivalent of stringByFoldingWithOptions:locale: in Java?

Related Related

  1. 1

    what is the equivalent of :: operator in D?

  2. 2

    what is the equivalent of += operator in clojure

  3. 3

    What is the BINARY operator equivalent in JPQL?

  4. 4

    What is the JavaScript equivalent for Swift ?? operator

  5. 5

    What is the BINARY operator equivalent in JPQL?

  6. 6

    What is the Java equivalent for LINQ?

  7. 7

    What is the 'defer' equivalent for Java

  8. 8

    What is the ruby equivalent of this java loop since there's no pre-increment operator?

  9. 9

    What is the equivalent of the join operator over a vector of Strings?

  10. 10

    What is the Python numpy equivalent of the IDL # operator?

  11. 11

    What's the equivalent of a spread operator (...) for objects properties?

  12. 12

    What is the equivalent of the "is" operator for reflected generic types?

  13. 13

    what is '^=' operator in java

  14. 14

    what is '^=' operator in java

  15. 15

    What is the purpose of "^= "operator in Java?

  16. 16

    What is "~" operator in Java

  17. 17

    Java equivalent for C++ operator overloading

  18. 18

    Java equivalent of C++ copy assignment operator

  19. 19

    Scala equivalent of Java 8 :: (double colon) operator

  20. 20

    Java equivalent for C++ operator overloading

  21. 21

    What is the Java equivalent of this Scala code?

  22. 22

    What is the equivalent of javascript setTimeout in Java?

  23. 23

    What is the equivalent of stringByFoldingWithOptions:locale: in Java?

  24. 24

    Java 8: What is the equivalent of "UseSplitVerifier"?

  25. 25

    What is the equivalent of Java Object in Swift?

  26. 26

    What is the Python equivalent of RequestConfig in Java?

  27. 27

    What is the Java equivalent of this Haskell function?

  28. 28

    What is the equivalent of Java Scanner in Kotlin?

  29. 29

    What is the equivalent of stringByFoldingWithOptions:locale: in Java?

HotTag

Archive