I am trying to learn how constructors work and I have been trying to debug this simple java program but I cannot get it to run. Eclipse simple refuses to acknowledge its presence and just runs an earlier project. Any ideas would be very gratefully received - I am struggling to see what I have done wrong.
package timber;
public class Person {
private String firstName;
private String lastName;
private String address;
private String username;
public Person(String personFirstName, String personLastName, String personAddress, String personUsername)
{
firstName = personFirstName;
lastName = personLastName;
address = personAddress;
username = personUsername;
}
public void displayPersonDetails()
{
System.out.println("Name: " + firstName + " " + lastName);
System.out.println("Address: " + address);
System.out.println("Username: " + username);
}
}
I then have a second class that contains the main method
package timber;
public class PersonExample {
public void main(String[] args) {
Person dave = new Person("Dave", "Davidson", "12 Main St.", "DDavidson");
dave.displayPersonDetails();
}
}
could you please add static in main method :-
public static void main(String[] args) {
Person dave = new Person("Dave", "Davidson", "12 Main St.", "DDavidson");
dave.displayPersonDetails();
}
Your main method always needs to be static.
See Why is the Java main method static?
Replace,
public void main(String[] args)
with
public static void main(String[] args)
Your main method needs to be 'static'. Why? If you want to call a method in Java, there are two ways
Create an instance of class and use that object to call it's methods
Make a method 'static' so that you can call the method using the class name and no instance creation needed.
The 'main' method, if non-static (like in your case) you have to create an instance of it's class to call. But, where would you create an instance of that class, where your main method is residing? Create another class and call from there? and create yet another class to call this class? It will never end. I mean there has to be a starting point right?
By giving the method the name 'main' and by adding the keyword 'static' in it's signature, you help JVM call that method without the need to an create instance.
Just change
public void main(String[] args)
to
public static void main(String[] args)
Everything is fine you have just missed the static keyword .
Instead of
public void main(String [] args)
Use
public static void main(String[] args)
Related
I have a class with static variables as:
class Commons {
public static String DOMAIN ="www.mydomain.com";
public static String PRIVATE_AREA = DOMAIN + "/area.php";
}
And if I try to change DOMAIN from an Android Activity (or another java class) at runtime, the DOMAIN variable change but PRIVATE_AREA don't change. Why?
This is because the assignment of static fields happens once the class is loaded (occurs only one time) into the JVM. The PRIVATE_AREA variable will not be updated when the DOMAIN variable is changed.
public class Test {
public static String name = "Andrew";
public static String fullName = name + " Barnes";
public static void main(String[] args){
name = "Barry";
System.out.println(name); // Barry
System.out.println(fullName); // Andrew Barnes
}
}
I suggest that you use the following structure.
public class Test {
private static String name = "Andrew";
public static String fullName = name + " Barnes";
public static void setName(String nameArg) {
name = nameArg;
fullName = nameArg + " Barnes";
}
}
Test2.java
public class Test2 {
public static void main(String[] args){
System.out.println(Test.fullName); // Andrew Barnes
Test.setName("Barry");
System.out.println(Test.fullName); // Barry Barnes
}
}
This is because Static variables are initialized only once , at the start of the execution.
See more at: http://www.guru99.com/java-static-variable-methods.html
PRIVATE_AREA did't change because it is set on declaration time. When You change DOMAIN, it has no effect on PRIVATE_AREA.
Maybe it is better to work with setter(...) and getter() Methods and local variables. On getting PRIVATE_AREA You create the retrun value again.
Assignment of variable happens at the load time of class thats why after that if you change the value of one static variable, it will not reflect there where it is assigned to another variable
i am new to java is there is any possible way of calling a function within same class without creating a object for the class my program is
public class Puppy{
public Pup(String name){
System.out.println("Passed Name is :" + name );
}
public static void main(String []args){
public Pup( "tommy" );
}
}
i want to call function pup without creating object for it ,is it possible?
You can declare the method to be static:
public static void Pup(String name){
...
}
As an aside: you should use standard Java naming conventions and start your method name with a lower-case letter:
public static void pup(String name){ ...
Try this:
public class Puppy{
public static void Pup(String name){
System.out.println("Passed Name is :" + name );
}
public static void main(String []args){
Pup("tommy");
}
}
You can define Pup as statis method like:
public static void Pup(String name){
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
And from main call it like:
Pup("tommy");
There are couple of issues in your code:
You don't have valid return type for your method Pup. If you dont return anything you should add void as return type.
You can't use public or any access modifier within any method. So you need remove public from method call.
I'm not sure but maybe....
Inside your main method, you can simply use this statement,
Pup("Your Name");
Your Pup method should have a valid return statement and use these modifiers,
public static
if it's a returning method
public static void
if it's a void method
I'm New to Java and I am following some instruction, However When I get to the Strings section
public class String {
public static void main(String[] args) {
java.lang.String name;
name = "luke";
System.out.println("Hello, " + name + "pleased to meet you");
}
}
But I Get
Error: Main method not found in class String, please define the main method as:
public static void main(String[] args)
If you insist on using String as your class name it should be:
public class String {
public static void main(java.lang.String[] args) {
java.lang.String name;
name = "luke";
System.out.println("Hello, " + name + "pleased to meet you");
}
}
I don't think it's particularly wise to try and re-use the names of classes defined in java.lang though.
Since your class is named String, it is being inferred by the compiler as the argument type of your main method.
Try fully qualifying the argument type instead:
public static void main(java.lang.String[] args) {
...
Or better yet, rename your class to use and non-java.lang class name.
You were careful to fully-qualify your reference to java.lang.String for the name variable, but not for the args parameter to main.
Use
public static void main(java.lang.String[] args) {
Of course, this all resulted because you named your class String, the same name as a built-in class in Java. Perhaps you could name it StringTest instead? That would avoid having to worry about class name collision and fully qualifying the Java built-in String.
As your class hides the java.lang.String name, you need to write
public static void main(java.lang.String[] args) {
Better call your class StringTest or something else to avoid this confusion.
public class StringTest {
public static void main(String[] args) {
String name = "luke";
System.out.println("Hello, " + name + "pleased to meet you");
}
}
why don't you try this
public class StringFirstTry {
public static void main(String[] args) {
String name;
name = "luke";
System.out.println("Hello, " + name + "pleased to meet you");
}
}
E.g: I got a Java class with the main function implemented like this:
public class Job{
public static void main(String[] args) throws Exception{
Job jobA = new Job();
String jobName = System.getProperty("JobName");
job.DoJobA("jobName");
}
public void DoJobA(String jobName){
String configPath = System.getProperty("ConFig");
File file = new File(configPath+ "/" + jobName);
DoJobB(file);
}
}
And I another class and want to call the main function of class Job but couldn't find a way to do that!
Is there any advise for me?
You should be able to call it like you call any other static method
Job.main(yourArgs);
If you make the main method use var args then you dont need to pass in any variable
public static void main(String... args) throws Exception{
...
}
then you can just call it as
Job.main();
if you need arguments then you can pass them if.
I have a simple problem that I've been stuck on for some time and I can't quite find the answer to. Basically, I'm creating an object and trying to access the variables without using static variables as I was told that is the wrong way to do it. Here is some example code of the problem. I receive an error in the first class that can not be resolved to a variable. What I would like to be able to do is access t.name in other methods outside of the main, but also in other classes as well. To get around this previously I would use Test2.name and make the variable static in the Test2 class, and correct me if I'm wrong but I believe that's the wrong way to do it. Any help would be greatly appreciated =)
public class Test {
public static void main(String[] args) {
Test2 t = new Test2("Joe");
}
public void displayName() {
System.out.println("Name2: " + t.name);
}
}
public class Test2 {
String name;
public Test2 (String nm) {
name = nm;
}
}
I see others have posted code snippets, but they haven't actually posted why any of this works (at the time of this writing.)
The reason you are getting a compilation error, is that in your method
public static void main(String[] args) {
Test2 t = new Test2("Joe");
}
Variable t's scope is just that method. You are defining Test2 t to only be in the main(String[] args) method, so you can only use the variable t in that method. However, if you were to make the variable a field, like so, and create a new instance of the Test class,
public class Test {
Test2 t;
public static void main(String[] args) {
Test test = new Test();
test.t = new Test2("Joe");
test.displayName();
}
public void displayName() {
System.out.println("Name2: " + t.name);
}
}
Then you should no longer be getting any compilation errors, since you are declaring the variable t to be in the class Test scope.
You may give the reference to your test object as an argument to method displayName:
public class Test {
public static void main(String[] args) {
Test2 t = new Test2("Joe");
displayName(t);
}
public static void displayName(Test2 test) {
System.out.println("Name2: " + test.name);
}
}
Note: I also made displayName a static method. From within your main method you can only access static methods without reference.
Modify the Test class to this
public class Test {
private static Test2 t;
public static void main(String[] args) {
t = new Test2("Joe");
}
public void displayName() {
System.out.println("Name2: " + t.name);
}
}
Use a getter for your purpose. This is a side solution to your problem, but generally this is how you should retrieve instance variables, using getters.
public class Test {
public static void main(String[] args) {
Test2 t = new Test2("Joe");
displayName(t);
}
public static void displayName(Test2 test) {
System.out.println(test.getName());
}
}
public class Test2
{
private String name;
public Test2 (String nm)
{
name = nm;
}
public String getName()
{
return name;
}
}
Always remember, variables in your class should be private. That protects it from access from outside the class. Hence, getters are the only way to access them. And setters or constructors to initialize them.
Fewer statics the better I reckon.
I would Instantiate Test and call displayName on the instance of it, then pass the instance of Test2 to displayName.
But it does depend on what the overall aim is