in Education by
I am trying to understand Static methods in java. I have done a basic python before, so I am trying to visualise the Java's static method in Python. Is the static method in the code below class test{ public static void main(String[] args) { printHello(); } public static void printHello(){ System.out.println("This is a Java's static method"); } } equivalent to this? def printhello(): print("It is a printhello function") class hello(): def printhello(self): print("It is a printhello method of a instance") As static methods can be accessed without creation of objects, def printhello would work like this. And when a object is accessing static method in Java, the instance method provided in the python class would work like that. If not, what would be right visualisation of a java's static method in python. Thanks. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Here class Hello: def printhello(self): ... you're passing a self reference to the method. This means it is an instance method. A static method never owns a reference to an instance of its containing class by default. Python class static methods are defined, usually, using the @staticmethod annotation, and they don't accept self class Hello: @staticmethod def printhello(): ... So the above would become, in Java class Hello { public static void printhello() { ... } } Java doesn't have the concept of first-level functions, so this def printhello(): print("It is a printhello function") isn't really translatable, but it's usually a more idiomatic way of defining free/static functions in Python. As you may have understood, answering your question Is the static method in the code below ... equivalent to this? No, it's not equivalent at all.

Related questions

0 votes
    I am trying to understand Static methods in java. I have done a basic python before, so I am ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 13, 2022 in Education by JackTerrance
0 votes
    What should be the execution order, if a class has a method, static block, instance block, and ... stackoverflow.com 🔗Source: Java Interview Questions and Answers...
asked Dec 19, 2020 in Technology by Editorial Staff
0 votes
    In which process, a local variable has the same name as one of the instance variables? a) Serialization ... stackoverflow.com 🔗Source: Java Interview Questions and Answers...
asked Dec 19, 2020 in Technology by Editorial Staff
0 votes
    Can abstract keyword be used with constructor, Initialization Block, Instance Initialization and Static Initialization ... Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Can abstract keyword be used with constructor, Initialization Block, Instance Initialization and Static Initialization ... Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    In the python documentation, an instance of a named_tuple is created by using the below code: Point = named_tuple(' ... of doing it? Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
0 votes
    Which of the following is a method having same name as that of it's class? (a) finalize (b) ... java programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    Which of the following is a method having same name as that of its class? (a) finalize (b) delete ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    What is the process of defining a method in a subclass having same name & type signature as a method ... questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    What are the Restrictions of the Static Method in JAVA?...
asked Dec 21, 2020 in Technology by JackTerrance
0 votes
    Which of these can be used to differentiate two or more methods having the same name? (a) Parameters ... questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    Can “this” be used within a static method?...
asked Mar 31, 2021 in Technology by JackTerrance
0 votes
    Using the WCF web programming model one can specify an operation contract like so: [OperationContract] [WebGet ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Ok, in hindsight this does seem to be a silly problem I got myself into and shouldn't have done ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
...