Access method without creating Object

0 votes
Is it possible to access a method without creating an Object for the same? I am new in Java and trying to learn. Can anyone help me with this?
Feb 6, 2019 in Java by Sanchita
4,549 views

1 answer to this question.

0 votes

This is not possible in java. You cannot call an method without an object.

What happens is basically this:-

public class MyClass {
  public void method1() {
    System.out.println("This is method1");
  }
  public void method2(int num) {
    System.out.println("This will print the value of num " + num);
  }
  public static void main(String[] args) {
    MyClass object1 = new MyClass();  
    object1.method1(); 
    object1.method2(200);
  }
}

This way you can access the methods.

answered Feb 7, 2019 by Dheeraj

Related Questions In Java

0 votes
1 answer

Running Java program without main method.

This would have worked fine till Java ...READ MORE

answered Nov 21, 2018 in Java by Anoop
622 views
0 votes
1 answer
+1 vote
1 answer
0 votes
0 answers

Data access object (DAO) in Java

I was going through a document and ...READ MORE

Apr 19, 2022 in Java by Rahul
• 3,380 points
589 views
0 votes
2 answers

One line initialization of an ArrayList object in Java

In Java 8 or earlier: List<String> string = ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,220 points
4,650 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,250 views
0 votes
1 answer

How to download and save a file from Internet using Java?

public void saveUrl(final String filename, final String ...READ MORE

answered May 25, 2018 in Java by Rishabh
• 3,620 points
873 views
+1 vote
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,290 points
1,143 views
+1 vote
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,680 points
3,676 views
0 votes
1 answer

Why Object is Super Class in Java?

Object is an exception to the first rule, ...READ MORE

answered Feb 7, 2019 in Java by Dheeraj
816 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP