• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java OCA problems

 
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Given
1.  class Person{
2.  Person(String s,int i ){
3.      ++pid;
4.      name=s;
5.      age=i;
6.      }
7.    static int pid;
8.    int age;
9.    String name;
10.      }
11.  class Test{
12.    public static void main(String args[]){
13.    Person p1 = new Person("John" , 22);
14.    Test te = new Test();
15.    Person p2 = te.change(p1);
16.    System.out.println(p2.pid +" "+p2.name+ " "+p2.age);
17.    System.out.print(p1.pid +" "+p1.name+" "+ p1.age);
18.  }
19.  private Person change(Object o){
20.  Person p2 = (Person)o;
21.  p2.age = 25;
22.  return p2;
23.  }
24.  }


Can someone please explain me above problem (copied from other site)

Created Person.java class file in Eclipse. When I try to execute following error displayed.

Error: Main method not found in class practice.Person, please define the main method as:
  public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

In one file two classes are mentioned. Line numbers suggests it should be a single file. Please explain.
 
author & internet detective
Posts: 41871
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aria,
Welcome to CodeRanch!

You are correct that this code can be in one file. However, that file needs to be named Test.java. The reason is that the main method is in that class to run.
 
Jeanne Boyarsky
author & internet detective
Posts: 41871
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, here is a link to the "other site" with the question. (We request mock exam questions be cited.)
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Aria, I'm glad that you could join us.

Two quick items.
  • It can be helpful to the rest of us if you use the codetags (https://coderanch.com/wiki/659781/Code-Tags) when posting code to this site.
  • If can he helpful to one to use the preview button, which is to the left of the submit button, to ensure that you have your post correct. I use this button all of the time.

  • Feel free to ask the community any Java (and some not Java) questions. I've found that everyone here is super helpful, nice, knowledgeable and welcoming.
    Not only is this a great place for beginners, but experts as well and anyone in between.
     
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello.
    I think that you need at least one public class to run your file, the file should have the public class name.
     
    Pete Letkeman
    Bartender
    Posts: 1868
    81
    Android IntelliJ IDE MySQL Database Chrome Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome to CodeRanch Ricardo.

    This code from Aria's initial post works or at least it runs without any runtime or compile time errors as long as the file is named Test.java
    Test.java contents:

    I'm not too sure why Aria was experiencing the error. Perhaps a error was made when copying the code and a brace bracket was missed?

    Ricardo Cardona wrote:I think that you need at least one public class to run your file, the file should have the public class name.


    I know that it can be confusing, however this is incorrect. You can only have one public class or one public interface in a file. However you can have many non public classes and non public interfaces in a file.
    The rule is that the class name with the  public static void main(String args[]) that you wish to call needs to be the name of the file.
    At least that is how I understand the rule.
    This program, which doesn't do too much works fine as long as the file name is MyClass.java when compiled using javac
    MyClass.java contents:

    One thing that I did not know until recently is that this code will work fine even with the keyword abstract:
    MyClass.java contents:

    All the code in this post will compile and run.
     
    Ricardo Cardona
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello Pete.
    Thanks for the answer.
    I tried to run the code and I realized that it might happen because he tries to run the generated .class file from the class that does not contain the main method.
    So what you say is true.
    It should execute the name of the class that contains the main method.
     
    Pete Letkeman
    Bartender
    Posts: 1868
    81
    Android IntelliJ IDE MySQL Database Chrome Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I know it's kind of hard to read code when it's not formatted too well, Ricardo.

    I'm going to take a wild guess that you are thinking about getting Java certified.

    If this is the case then you may want to look as what others have done as posted here:
    https://coderanch.com/wiki/659980/Ocajp-Wall-Fame

    And this is what I'm currently how I'm preparing:
    https://coderanch.com/t/684504#3212329

    However if you are thinking about the OCP exam then you may want to look at what these people did:
    https://coderanch.com/wiki/659976/Ocpjp-Wall-Fame

    This could save you some time, money, effort and frustration.
     
    Ricardo Cardona
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In fact, Pete.
    I'll take the OCA, I'm a little scared about it.
    I have 4 years of university with Java, I think I am prepared, yet I downloaded the book Oracle Certified Associate Java SE 8 Programmer study guide exam by Jean Boyarsky.
    I'll prepare for a couple of months and then I'll try.
    Thanks for answer.
     
    Pete Letkeman
    Bartender
    Posts: 1868
    81
    Android IntelliJ IDE MySQL Database Chrome Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I'm glad I was able to help out Ricardo.

    Remember, there are a lot of people who have difficulty with the test as noted near the bottom of this post https://coderanch.com/t/684360#3211440 by Roel De Nijs.

    I hope that you'll find that there are many people on this site who are willing to help and who are well versed in Java. So ask all the Java questions you want, and some one will probably be able to help you out.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic