Starting ex. 5.
16:22
Java:
---
// UseStorage.java
// chapter 2, ex 5class UseStorage {
static int storage(String s) {
return s.length() * 2;
}
public static void main( String[] args ) {
int s = storage( args[ 0 ] );
System.out.println( "storage for \"" + args[ 0 ] + "\" is " + s );
}
}
---
Gosh, that was tough. (Kidding!)
16:27
---
;;;; chapter 2, ex 5
(defun storage (s)
(* 2 (length s)))
(defun use-storage (arg)
(let ((s (storage arg)))
(format t "storage for \"~a\" is ~d~%" arg s)))
---
??:??
Chapter 2, ex. 5
Java:
--
// StaticFun.java
// chapter 2, ex 6class StaticTest {
static int i = 47;
}class StaticFun {
static void incr() { StaticTest.i++; }public static void main( String[] args ) {
StaticFun sf = new StaticFun();
sf.incr();
StaticFun.incr();
}
}
---
18:34
Lisp:
---
;;;; chapter 2, ex 6
(defclass static-test ()
((i :allocation :class :initform 47 :accessor static-test-i)))
(defclass static-fun ())
(defmethod incr ((st static-test))
(incf (static-test-i st)))
(defun static-fun-main ()
(let ((sf (make-instance 'static-test)))
(incr sf)
; no CLOS analogue to static function
))
---
I looked all around, and can't find a way to access a shared slot without having a reference to an object of the class.