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 in Java by Sanchita
23 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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 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
21 views
0 votes
1 answer
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,190 points
81 views
0 votes
2 answers

How do I read and convert an InputStream object to string type?

You can also use Java Standard Library ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,820 points
1,509 views
0 votes
3 answers

Convert date object to a String

We parse the full date to time ...READ MORE

answered Jul 30, 2018 in Java by samarth295
• 2,190 points
43 views
0 votes
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,520 points
34 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 24, 2018 in Java by Rishabh
• 3,520 points
26 views
0 votes
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,280 points
22 views
0 votes
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,320 points
201 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 in Java by Dheeraj
25 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.