Results 1 to 6 of 6
Like Tree4Likes
  • 1 Post By jim829
  • 3 Post By Tolls

Thread: This business about non-static calling!

  1. #1
    AmericanPi is offline Member
    Join Date
    Dec 2016
    Posts
    8
    Rep Power
    0

    Default This business about non-static calling!

    Please use the code i have here for yourself if you please, it is Free to use Software!

    So this is how I think it is supposed to work
    Java Code:
    //one public class to keep the entire program in
    public class Day1Exericise {
    
         //one main() to put all method calls and Object Instantiations in
         public static void main (String[] args) {
         className objectName = new className();
         }//end main()
    
         //and then all the class with appropriate methods for calling in main() and Instantiating Objects from in main()
         public className {
              method1();
              method2();
         }//end className
    
    }//end-Day1Exercise
    But I got codes like this for a while.
    Day1Exercise.java:12: error: non-static variable this cannot be referenced from a static context
    Investing investment = new Investing();
    Is the above correct?

    I need to really get this down. If I could just understand this I could understand Java!
    I get it's about hierarchy and stuff but I guess I'm doing it wrong.


    Here's a program for investing I made (LOL when i first compiled it there were 27 errors. hahaha I learned so much figuring those out)::::
    *the variables I pass as Parameters in main() are Declared and Instantiated in main()
    *the variables I use in my Methods are Declared and Instantiated in the Classes they're from

    This code throws tell me" public class Investing" is an illegal start of an expression. (not nice!)

    Java Code:
    public class Day1Exercise { 
    
    		
    	
    public static void main (String[] args) { 
    	
    		int initialInvestment = 14000;
    		double firstYearIncrease = 0.4;
    		int secondYearLoss = 1500;
    		double thirdYearIncrease = 0.12;
    	
    		Investing investment = new Investing();
    		
    		System.out.println("My investment's value after the 1st year is " + investment.firstYear(initialInvestment, firstYearIncrease));
    		System.out.println("My investment's vlaue after the 2nd year is " + investment.secondYear(firstYearEndValue, secondYearLoss));
    		System.out.println("My investment's value after the 3rd year is " + investment.thirdYear(secondYearEndValue, thirdYearIncrease));
    		
    		System.out.println("Thank you for using GNU/Linux!");
    	
    
    public class Investing {
    
    	double firstYearEndValue = 0.0;
    	double secondYearEndValue = 0.0;
    	double thirdYearEndValue = 0.0;
    	
    	double firstYear(int investment, double inc_dec) {
    		firstYearEndValue = investment + (investment * inc_dec);
    	return firstYearEndValue;
    	}//end-firstYear
    	
    	double secondYear(int endValue, int inc_dec) {
    		secondYearEndValue = (endValue - inc_dec);
    	return secondYearEndValue;
    	}end-secondYear
    	
    	double thirdYear(int endValue, double inc_dec) {
    		thirdYearEndValue = (endValue * inc_dec);
    	return thirdYearEndValue;
    	}end-thirdYear
    
    }//end-Investing
    
    return;
    	}//end-Main
    
    }//end-Day1Exercise
    Last edited by AmericanPi; 12-19-2016 at 10:59 PM. Reason: wanted to tell otheres to use the code and i added a question.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: This business about non-static calling!

    I am not certain what you are trying to do or show. Your first example has lots of errors. Incorrect inner class designation. Incorrect method signatures.

    In your last example, you need to move the class declaration before you use it within the main method. And you need to qualify the instance variables with an
    instantiated instance of the class.

    With regard to static vs non-static.

    You cannot reference an instance field from a static context (i.e. a static method or static nested class)
    unless it is qualified with an instance of the containing class. Here are some examples :

    Java Code:
    public class TestClass {
       int instance_value = 10;
       static int static_value = 20;
       public static void main(String[] args) {
         System.out.println(instance_value);// error, can't access from static method
        
         // Solution
         TestClass tc = new TestClass();
         System.out.println(tc.instance_value); // qualified access
         System.out.println(static_value);// static value from static method
       }
       
       public void method() {
          System.out.println(instance_value); // okay from instance method
          System.out.println(static_value); // static value from instance method okay here too.
          TestClass tc = new TestClass();
          System.out.println(tc.static_value); // works but not proper technique
          // Preferred way to access static_values which might be in other classes
          // and need to be qualified.
          System.out.println(TestClass.static_value); 
       }
    }
    Regards,
    Jim
    AmericanPi likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: This business about non-static calling!

    Until you are confident about coding in Java I would recommend having each class in its own file, otherwise you end up in this situation with inner classes and the rules around them, none of which will help you with learning the basics.
    SurfMan, jim829 and AmericanPi like this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    AmericanPi is offline Member
    Join Date
    Dec 2016
    Posts
    8
    Rep Power
    0

    Default Re: This business about non-static calling!

    Quote Originally Posted by jim829 View Post
    I am not certain what you are trying to do or show. Your first example has lots of errors. Incorrect inner class designation. Incorrect method signatures.

    In your last example, you need to move the class declaration before you use it within the main method. And you need to qualify the instance variables with an
    instantiated instance of the class.

    With regard to static vs non-static.

    You cannot reference an instance field from a static context (i.e. a static method or static nested class)
    unless it is qualified with an instance of the containing class. Here are some examples :
    I don't think I knew what i was trying to do either.
    All the rules you mentioned i know. I must have confused myself.
    NOW THAT LAST ONE!!! Is definately an 'Ohhhhhhh" moment. and explains allot.
    TX for the code template Jim! That's gonna be a great reference for me.

    I went back last night and fixed my code. It's at the end of this thread.
    tx again.

  5. #5
    AmericanPi is offline Member
    Join Date
    Dec 2016
    Posts
    8
    Rep Power
    0

    Default Re: This business about non-static calling!

    Quote Originally Posted by Tolls View Post
    Until you are confident about coding in Java I would recommend having each class in its own file, otherwise you end up in this situation with inner classes and the rules around them, none of which will help you with learning the basics.
    Oh man are you right!
    I noticed my code wasn't like the exercises in the book I'm learning from (Sam's teach yourself Java 6 in 21 days).
    And my problem was just like you said so what i did was.....
    1) made a new directory just for this program
    2) made a class (as it's own .java file in that dir) with the methods for evaluating interest and stuff (this is the app that serves those functions only.
    3) made a class (as it's own .java file in the same dir) just for calling main() (this is file I run when I want to run the program).

    so I figure I'll do that every-time, make a file for the program I'm making and put every thing in there as separate .java files (classes).
    As far as what Jim said about....
    "You cannot reference an instance field from a static context (i.e. a static method or static nested class)
    unless it is qualified with an instance of the containing class. Here are some examples :"
    ....is that what i did when I made a Class Variable in main and assigned the other files Method's Return Value?

    Hey TX Tolls. I appreciate your help too.

  6. #6
    AmericanPi is offline Member
    Join Date
    Dec 2016
    Posts
    8
    Rep Power
    0

    Default Re: This business about non-static calling!

    Here's my working code with adjustments made using Jim and Toll's Advice
    Java Code:
    //Increase_Loss.java in directory Java_Applications
    public class Increase_Loss { 
    
    	private double firstYearEndValue = 0.0;
    	private double secondYearEndValue = 0.0;
    	private double thirdYearEndValue = 0.0;
    	
    	double firstYear(double investment, double inc_dec) {
    		firstYearEndValue = investment + (investment * inc_dec);
    	return firstYearEndValue;
    	}
    	
    	double secondYear(double endValue, double inc_dec) {
    		secondYearEndValue = (endValue - inc_dec);
    	return secondYearEndValue;
    	}
    	
    	double thirdYear(double endValue, double inc_dec) {
    		thirdYearEndValue = (endValue * inc_dec);
    	return thirdYearEndValue;
    	}	
    	
    }//end of Day1Exercise
    These are two DIFFERENT .java files, that are in the same Directory.
    Java Code:
    //my main() class, CallInvestmentInfo in directory Java_Applications
    public class CallInvestmentInfo {	
    	public static void main (String[] args) { 
    	
    		double initialInvestment = 14000.0;
    		double firstYearIncrease = .4;
    		double secondYearLoss = 1500.0;
    		double thirdYearIncrease = .12;
    	
    		double firstYearVal = .0;
    		double secondYearVal = .0;
    		double thirdYearVal = .0;
    		
    		Increase_Loss investment = new Increase_Loss();
    		System.out.println();
    		
    		firstYearVal = investment.firstYear(initialInvestment, firstYearIncreaase);
    		System.out.println("My investment's value at the end of the 1st year is " + firstYearVal);
    	
    		secondYearVal = investment.secondYear(firstYearVal, secondYearLoss);
    		System.out.println("My investment's value at the end of the 2nd year is " + secondYearVal);
    		
    		thirdYearVal = investment.thirdYear(secondYearVal, thirdYearIncrease);
    		System.out.println("My investment's value at the end of the 3rd year is " + thirdYearVal);
    		
    		System.out.println();
    		System.out.println("Thank you for using GNU/Linux!");
    		
    		
    	return;
    	}//end of main
    
    }//end of CallInvestmentInfo
    Last edited by AmericanPi; 12-20-2016 at 09:42 PM. Reason: code tags wont work

Similar Threads

  1. Calling static methods
    By Prime624 in forum New To Java
    Replies: 4
    Last Post: 02-10-2013, 08:30 PM
  2. Calling a non-static method out of a package
    By Kaffee in forum New To Java
    Replies: 2
    Last Post: 10-03-2011, 12:00 AM
  3. Need help with calling a static method please.
    By toppcon in forum New To Java
    Replies: 12
    Last Post: 07-07-2011, 05:20 AM
  4. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum Advanced Java
    Replies: 3
    Last Post: 01-24-2009, 12:58 PM
  5. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 05:10 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
  •