Answers for "use class variable in method java"

1

using class in java

class Number {
    	
    	int number;
    	
    	// square numbers
    	public boolean isSquare(){
    		
    		double squareRoot = Math.sqrt(number);
    		
    		
    		if(squareRoot == Math.floor(squareRoot))
    		{
    			return true;
    		} else {
    			return false;
    		}
    		
    	}
    	
    	// triangular numbers
    	public boolean isTriangular(){
    		
    		int x = 1;
    		
    		int triangularNumber = 1;
    		
    		while(triangularNumber < number){
    			
    			x++;
    			triangularNumber = triangularNumber + x;
    			
    		}
    		
    		if(triangularNumber == number){
    			return true;
    		} else {
    			return false;
    		}
    	}
    }
    
    // testing 
    Number myNumber = new Number();
    myNumber.number = 16;
    
    System.out.println(myNumber.isSquare());
Posted by: Guest on March-11-2020
0

what is class variable in java

Class variables also known as static
variables are declared with the static
keyword in a class, but outside a method,
constructor or a block. There would 
only be one copy of each class variable
per class, regardless of how many 
objects are created from it.


Instance variables 
are declared in a class, but outside 
a method. When space is allocated for 
an object in the heap, a slot for each 
instance variable value is created. 
Instance variables hold values that must
be referenced by more than one method, 
constructor or block, or essential parts
of an object's state that must be present
throughout the class.


Local variables are declared in methods,
constructors, or blocks. Local variables 
are created when the method, constructor 
or block is entered and the variable will
be destroyed once it exits the method, 
constructor, or block.
Posted by: Guest on January-22-2021

Code answers related to "use class variable in method java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language