Results 1 to 7 of 7

Thread: Coding problem

  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Coding problem

    hey guys I am making a code to say the height weight and age, but it said that non static method cant be referenced from a static method. This was so confusing, but worked when I changed my method to static. Can you guys explain my the method person has to be static? Does that always apply when you are running methods within the main method? here is code:
    Java Code:
    class person {
    	person mark = new person();
    	static void person(int age, int weight, int height) {
        System.out.println(age);
    System.out.println(weight);
    System.out.println(height);
    	public static void main(String[] args) {
    		person(5,73,40);	}
    	
    }
    Thanks guys

  2. #2
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Coding problem

    I understand what you are trying to do. Here are a few suggestions:

    1. Begin your class name with a capital letter. This isn't required by the Java language but it is a good convention to keep yourself straight.
    2. This line of code calls the constructor for person, person mark = new person(); but it doesn't allow you to initialize any of the values that it will have
    3. This line of code is calling a method that has the same name as the class, but it isn't creating any objects. person(5,73,40);

    Java Code:
    class Person {
    	
    	// instance variables
    	private int age;
    	private int weight;
    	private int height;
    
    	// constructor
    	public Person(int age, int weight, int height) {
    		this.age = age;
    		this.weight = weight;
    		this.height = height;
    		System.out.println(age);
    		System.out.println(weight);
    		System.out.println(height);
    	}
    
    	public static void main(String[] args) {
    		// this line of code creates one Person object called "mark"
    		Person mark = new Person(5,73,40);    
    	}
    
    }
    I suggest that you look at this Java tutorial to understand classes and objects:
    http://www.tutorialspoint.com/java/j...ct_classes.htm

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Coding problem

    It's no use, kaydell2. This one's determined to not go through any tutorial.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Coding problem

    Quote Originally Posted by DarrylBurke View Post
    It's no use, kaydell2. This one's determined to not go through any tutorial.

    db
    Lol no db it's just that I have difficulty understanding those tutorials... Thank you kaydell

  5. #5
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Coding problem

    I can suggest you take a look here if you find those oracle tutorials too complex:
    Introduction to Computer Science using Java
    I like likes!

  6. #6
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Coding problem

    Youtube is also pretty win for learning about java, i stopped using the java tutorials to get the beginner stuff down and watched youtube videos, specifically ones posted by a guy named "thenewboston" i found them pretty helpfull to get the basics of the language down and now i understand the http://http://docs.oracle.com/javase/tutorial/ alot better.

    my apoligies if im not supposed to mention a specific person on youtube, just was the ones i found most comprehendable for a newbie

    Russ

  7. #7
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

Similar Threads

  1. The problem with the coding xml
    By torsion in forum CLDC and MIDP
    Replies: 0
    Last Post: 06-05-2010, 03:52 AM
  2. Coding Problem
    By mfaulhaber in forum New To Java
    Replies: 2
    Last Post: 11-16-2009, 11:44 PM
  3. Hi folks, I have same problem with coding!!!
    By PVL268 in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 12:08 AM
  4. Help On Coding problem
    By mandrake446 in forum New To Java
    Replies: 3
    Last Post: 12-08-2007, 07:01 AM
  5. Problem in my coding
    By one198 in forum New To Java
    Replies: 9
    Last Post: 08-09-2007, 10:07 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •